0% found this document useful (0 votes)
6 views13 pages

Advanced Java Programming Lab Manual

The document is a lab manual for Artificial Intelligence, detailing various Java programming concepts including socket programming, applet programming, multi-threading, JavaBeans, JSP for data insertion, form validation, user authentication, and cookie management. Each section includes sample code demonstrating the respective concept. The manual is intended for students in the Computer Science and Engineering department at Trinity Institute of Innovations in Professional Studies, Greater Noida.

Uploaded by

keertitha20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Advanced Java Programming Lab Manual

The document is a lab manual for Artificial Intelligence, detailing various Java programming concepts including socket programming, applet programming, multi-threading, JavaBeans, JSP for data insertion, form validation, user authentication, and cookie management. Each section includes sample code demonstrating the respective concept. The manual is intended for students in the Computer Science and Engineering department at Trinity Institute of Innovations in Professional Studies, Greater Noida.

Uploaded by

keertitha20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lab Manual

Artificial Intelligence

1
Department of Computer Science and Engineering
Trinity Institute of Innovations In Professional Studies

Greater Noida
Affiliated to
GGSIP University
New Delhi

2
CONTENTS

S.No Java Program Description

Demonstrates communication between client and server using


1 Socket Programming
sockets.

2 Applet Programming Demonstrates how to create and display a simple Java applet.

Demonstrates multi-threading by running multiple threads


3 Multi-threading
concurrently.

4 Applet Concept Shows basic applet rendering with graphics.

5 Java Beans Demonstrates JavaBeans by creating a simple bean class.

6 JSP Insert Data Inserts data into a database table using JSP and JDBC.

7 JSP Form Validation Implements client-side form validation in JSP using JavaScript.

User Validation
8 Shows user authentication using Servlet and database.
(Servlet)

9 Set Cookie (Servlet) Demonstrates how to set and retrieve cookies using Servlet.

Web Program Develops a small web application with Servlets, JSP, and database
10
(Servlet + JSP + DB) connectivity.

Program 1. Write a Java program to demonstrate the concept of socket


programming
3
import java.io.*;
import java.net.*;

// Server Program
class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Server started...");
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Client says: " + in.readLine());
serverSocket.close();
}
}

// Client Program
class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 5000);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("Hello, Server!");
socket.close();
}

4
Program 2. Write a Java program to demonstrate the concept of applet
programming.

import java.applet.*;
import java.awt.*;

// Applet Program
public class MyApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello, Applet!", 50, 50);

5
Program3.
Write a Java program to demonstrate the concept of multi-threading.

class MyThread extends Thread {


public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " is running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class MultiThreadDemo {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.start();
t2.start();
}
}

6
Program4. Write a Java program to demonstrate the concept of applet.

import java.applet.*;
import java.awt.*;

// Applet Program
public class MyApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello, Applet!", 50, 50);
}
}

7
Program 5. Write a Java program to demonstrate the use of Java Beans.

import java.io.Serializable;

public class MyBean implements Serializable {


private String message;

public MyBean() {}

public String getMessage() {


return message;
}

public void setMessage(String message) {


this.message = message;
}
}

8
Program6. Write a Java program to insert data into a table using JSP.

<%@ page import="java.sql.*" %>


<html>
<body>
<%
String name = request.getParameter("name");
String email = request.getParameter("email");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password");
PreparedStatement ps = con.prepareStatement("INSERT INTO users(name, email) VALUES(?, ?)");
ps.setString(1, name);
ps.setString(2, email);
ps.executeUpdate();
out.println("Data Inserted Successfully");
} catch (Exception e) {
out.println(e);
}

9
Program7. Write JSP program to implement form data validation.

<html>
<head>
<script>
function validateForm() {
var name = document.forms["myForm"]["name"].value;
var email = document.forms["myForm"]["email"].value;
var emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

if (name == "") {
alert("Name must be filled out");
return false;
}
if (email == "" || !emailPattern.test(email)) {
alert("Please enter a valid email");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="insert.jsp" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

10
Program 8. Write a Java program to show user validation using Servlet.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class UserValidationServlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root",
"password");
PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE username=?
AND password=?");
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
out.println("<h3>Login Successful</h3>");
} else {
out.println("<h3>Invalid Username or Password</h3>");
}
} catch (Exception e) {
out.println(e);}
}
}

11
Program 9.Write a program to set cookie information using Servlet.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SetCookieServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

// Creating a cookie
Cookie userCookie = new Cookie("username", "JohnDoe");
userCookie.setMaxAge(60 * 60 * 24); // Cookie valid for one day
response.addCookie(userCookie);

out.println("<h3>Cookie has been set for the user.</h3>");


}
}

12
Program10.Develop a small web program using Servlets, JSPs with Database
connectivity

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Servlet to handle user login


public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

String username = request.getParameter("username");


String password = request.getParameter("password");

try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root",
"password");
PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE username=?
AND password=?");
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();

if (rs.next()) {
out.println("<h3>Login Successful</h3>");
} else {
out.println("<h3>Invalid Username or Password</h3>");
}
} catch (Exception e) {
out.println(e);
}
}
}

13

You might also like