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
[Link] 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 [Link].*;
import [Link].*;
// Server Program
class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(5000);
[Link]("Server started...");
Socket socket = [Link]();
BufferedReader in = new BufferedReader(new InputStreamReader([Link]()));
[Link]("Client says: " + [Link]());
[Link]();
}
}
// Client Program
class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 5000);
PrintWriter out = new PrintWriter([Link](), true);
[Link]("Hello, Server!");
[Link]();
}
4
Program 2. Write a Java program to demonstrate the concept of applet
programming.
import [Link].*;
import [Link].*;
// Applet Program
public class MyApplet extends Applet {
public void paint(Graphics g) {
[Link]("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++) {
[Link]([Link]().getName() + " is running");
try {
[Link](1000);
} catch (InterruptedException e) {
[Link](e);
}
}
}
}
public class MultiThreadDemo {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
[Link]();
[Link]();
}
}
6
Program4. Write a Java program to demonstrate the concept of applet.
import [Link].*;
import [Link].*;
// Applet Program
public class MyApplet extends Applet {
public void paint(Graphics g) {
[Link]("Hello, Applet!", 50, 50);
}
}
7
Program 5. Write a Java program to demonstrate the use of Java Beans.
import [Link];
public class MyBean implements Serializable {
private String message;
public MyBean() {}
public String getMessage() {
return message;
}
public void setMessage(String message) {
[Link] = message;
}
}
8
Program6. Write a Java program to insert data into a table using JSP.
<%@ page import="[Link].*" %>
<html>
<body>
<%
String name = [Link]("name");
String email = [Link]("email");
try {
[Link]("[Link]");
Connection con = [Link]("jdbc:mysql://localhost:3306/mydb", "root", "password");
PreparedStatement ps = [Link]("INSERT INTO users(name, email) VALUES(?, ?)");
[Link](1, name);
[Link](2, email);
[Link]();
[Link]("Data Inserted Successfully");
} catch (Exception e) {
[Link](e);
}
9
Program7. Write JSP program to implement form data validation.
<html>
<head>
<script>
function validateForm() {
var name = [Link]["myForm"]["name"].value;
var email = [Link]["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 == "" || ) {
alert("Please enter a valid email");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="[Link]" 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 [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class UserValidationServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
String username = [Link]("username");
String password = [Link]("password");
try {
[Link]("[Link]");
Connection con = [Link]("jdbc:mysql://localhost:3306/mydb", "root",
"password");
PreparedStatement ps = [Link]("SELECT * FROM users WHERE username=?
AND password=?");
[Link](1, username);
[Link](2, password);
ResultSet rs = [Link]();
if ([Link]()) {
[Link]("<h3>Login Successful</h3>");
} else {
[Link]("<h3>Invalid Username or Password</h3>");
}
} catch (Exception e) {
[Link](e);}
}
}
11
Program [Link] a program to set cookie information using Servlet.
import [Link].*;
import [Link].*;
import [Link].*;
public class SetCookieServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
// Creating a cookie
Cookie userCookie = new Cookie("username", "JohnDoe");
[Link](60 * 60 * 24); // Cookie valid for one day
[Link](userCookie);
[Link]("<h3>Cookie has been set for the user.</h3>");
}
}
12
[Link] a small web program using Servlets, JSPs with Database
connectivity
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
// Servlet to handle user login
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
String username = [Link]("username");
String password = [Link]("password");
try {
[Link]("[Link]");
Connection con = [Link]("jdbc:mysql://localhost:3306/mydb", "root",
"password");
PreparedStatement ps = [Link]("SELECT * FROM users WHERE username=?
AND password=?");
[Link](1, username);
[Link](2, password);
ResultSet rs = [Link]();
if ([Link]()) {
[Link]("<h3>Login Successful</h3>");
} else {
[Link]("<h3>Invalid Username or Password</h3>");
}
} catch (Exception e) {
[Link](e);
}
}
}
13