AJP Practical File
AJP Practical File
Practical: 1
AIM: Write a program to create simple JDBC Application to retrieve
records from Database (resultSet).
Code:
import java.sql.*;
class RetrieveRecord
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver is loaded and registered");
System.out.println("Connection is established");
while(rs.next())
{
rs.close();
st.close();
con.close();
LIT/CSE/2024-25 1
220860131021 Advanced Java Programming (3160707)
Output:
LIT/CSE/2024-25 2
220860131021 Advanced Java Programming (3160707)
Practical: 2
AIM: Write a program to create simple JDBC Application using
Prepared Statement insert and delete data from database.
Code:
import java.sql.*;
import java.util.*;
import java.io.*;
class preparedstatement
{
int i,id;
float cpi;
String sname;
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Connection established");
do
System.out.println("1.Insert");
System.out.println("2.Delete");
LIT/CSE/2024-25 3
220860131021 Advanced Java Programming (3160707)
System.out.println("0.Exit");
i= sc.nextInt();
if(i==1)
id = sc.nextInt();
cpi = sc.nextFloat();
ps1.setInt(1,id);
ps1.setString(2,sname);
ps1.setFloat(3,cpi);
ps1.executeUpdate();
System.out.println("Data is Inserted");
else if(i==2)
id = sc.nextInt();
ps2.setInt(1,id);
ps2.executeUpdate();
System.out.println("Selected row is deleted");
else if(i!=0)
System.out.println("Invalid Choice..!!");
LIT/CSE/2024-25 4
220860131021 Advanced Java Programming (3160707)
else
System.out.println("Exit..!!");
}while(i!=0);
sc.close();
ps2.close();
ps1.close();
con.close();
Output:
LIT/CSE/2024-25 5
220860131021 Advanced Java Programming (3160707)
LIT/CSE/2024-25 6
220860131021 Advanced Java Programming (3160707)
Practical: 3
AIM: Write a program to create simple JDBC Application using
callable statement to retrieve data from database (make use of stored
procedure).
Code for Stored Procedure proc1:
CREATE OR REPLACE PROCEDURE proc1 (emp_id IN NUMBER, emp_name
OUT VARCHAR) AS
BEGIN
FROM EMP9
WHERE ID = emp_id;
END proc1;
Code:
import java.sql.*;
class Callablestatement
Class.forName("oracle.jdbc.driver.OracleDriver");
LIT/CSE/2024-25 7
220860131021 Advanced Java Programming (3160707)
System.out.println("Connection established");
cst.registerOutParameter(2,Types.VARCHAR);
cst.setInt(1,102);
cst.execute();
cst.close();
con.close();
Output:
LIT/CSE/2024-25 8
220860131021 Advanced Java Programming (3160707)
Practical: 4
AIM: Write a program of web application to explore the concept of
sharing the resource (connection object) across multiple servlets of the
same web application (ServletConfig & ServletContext example).
Code:
Data.html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Employee Details</title>
</head>
<body>
<center>
<h1>Personal Details</h1>
</form>
</center>
</body>
</html>
LIT/CSE/2024-25 9
220860131021 Advanced Java Programming (3160707)
Web.xml file:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID"
version="4.0">
<servlet>
<servlet-name>Storage</servlet-name>
<servlet-class>StorageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Storage</servlet-name>
</servlet-mapping>
<servlet>
<servlet-name>Retrieve</servlet-name>
<servlet-class>RetrieveServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Retrieve</servlet-name>
</servlet-mapping>
</web-app>
LIT/CSE/2024-25 10
220860131021 Advanced Java Programming (3160707)
StorageServlet.java file:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
res.setContentType("text/html");
ServletContext sc = getServletContext();
sc.setAttribute("email2",email);
sc.setAttribute("phone2",phone);
out.println("<html>");
out.println("<body>");
out.println("</body>");
out.println("</html>");
out.close();
LIT/CSE/2024-25 11
220860131021 Advanced Java Programming (3160707)
RetrieveServlet.java file:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
res.setContentType("text/html");
ServletContext sc = getServletContext();
out.println("<html>");
out.println("<body>");
out.println("<h3>Email id:"+email3+"</h3>");
out.println("<h3>Phone number:"+phone3+"</h3>");
out.println("</body>");
out.println("</html>");
out.close();
LIT/CSE/2024-25 12
220860131021 Advanced Java Programming (3160707)
Output:
LIT/CSE/2024-25 13
220860131021 Advanced Java Programming (3160707)
Practical: 5
AIM: Write a program of web application to implement state
management using cookies.
Code:
CookieExample.html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie Example</title>
</head>
<body>
</form>
</body>
</html>
Welcome.html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
LIT/CSE/2024-25 14
220860131021 Advanced Java Programming (3160707)
</body>
</html>
Web.xml file:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID"
version="4.0">
<servlet>
<servlet-name>Create</servlet-name>
<servlet-class>CreateCookie</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Create</servlet-name>
<url-pattern>/create</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Check</servlet-name>
<servlet-class>CheckCookie</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Check</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping>
</web-app>
LIT/CSE/2024-25 15
220860131021 Advanced Java Programming (3160707)
CreateCookie.java file:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
res.setContentType("text/html");
res.addCookie(c);
out.println("<html>");
out.println("<body bgcolor= wheat><center>");
req.getRequestDispatcher("/Welcome.html").include(req,res);
out.println("</center></body>");
out.println("</html>");
out.close();
LIT/CSE/2024-25 16
220860131021 Advanced Java Programming (3160707)
CheckCookie.java file:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
res.setContentType("text/html");
out.println("<html>");
for(int i=0;i<c.length;i++)
{
Cookie c1 = c[i];
if(c1.getName().equals("user"))
break;
}
out.close();
LIT/CSE/2024-25 17
220860131021 Advanced Java Programming (3160707)
Output:
LIT/CSE/2024-25 18
220860131021 Advanced Java Programming (3160707)
Practical: 6
AIM: Write a program of web application to implement session
management using session API.
Code:
ShoppingPage.html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Shop Page</title>
</head>
<body bgcolor= "#f9f9f9">
<center>
</select><br><br>
Product Name:
</font>
LIT/CSE/2024-25 19
220860131021 Advanced Java Programming (3160707)
</center>
</form>
</body>
</html>
Web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID"
version="4.0">
<servlet>
<servlet-name>Shopping</servlet-name>
<servlet-class>ShopServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Shopping</servlet-name>
<url-pattern>/shopsession</url-pattern>
</servlet-mapping>
</web-app>
ShopServlet.java file:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
LIT/CSE/2024-25 20
220860131021 Advanced Java Programming (3160707)
HttpSession session;
String pCode,pname,clickButton;
res.setContentType("text/html");
session = req.getSession(true);
clickButton = req.getParameter("submit");
if(clickButton.equals("ADD ITEM"))
pCode = req.getParameter("pCode");
pname = req.getParameter("pname");
if(!(pCode.equals("")|| pname.equals("")))
session.setAttribute(pCode,pname);
res.sendRedirect("ShoppingPage.html");
pCode = req.getParameter("pCode");
session.removeAttribute(pCode);
res.sendRedirect("ShoppingPage.html");
LIT/CSE/2024-25 21
220860131021 Advanced Java Programming (3160707)
Enumeration e = session.getAttributeNames();
if(e.hasMoreElements())
while(e.hasMoreElements())
out.println("<body bgcolor=#F7F7F7>");
out.println("<h2>Product Code:"+code+"<br>");
out.println("Product
Name:"+session.getAttribute(code)+"</h2>");
else
{
out.println("<body bgcolor= #E0F2F1>");
}
}
else if(clickButton.equals("LOGOUT"))
session.invalidate();
res.sendRedirect("ShoppingPage.html");
LIT/CSE/2024-25 22
220860131021 Advanced Java Programming (3160707)
Output:
LIT/CSE/2024-25 23
220860131021 Advanced Java Programming (3160707)
LIT/CSE/2024-25 24
220860131021 Advanced Java Programming (3160707)
Practical: 7
AIM: Write a program for simple JSP application (using scripting
elements & implicit object).
Code:
Home.html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Invoke JSP Parameter</title>
</head>
<body>
</form>
</body>
</html>
Web.xml file:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID"
version="4.0">
<context-param>
<param-name>param1</param-name>
<param-value>param1</param-value>
</context-param>
LIT/CSE/2024-25 25
220860131021 Advanced Java Programming (3160707)
<servlet>
<servlet-name>myjsp</servlet-name>
<jsp-file>/Other.jsp</jsp-file>
<init-param>
<param-name>count</param-name>
<param-value>10</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myjsp</servlet-name>
<url-pattern>/other</url-pattern>
</servlet-mapping>
</web-app>
Request.jsp file:
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Invoke JSP</title>
</head>
<body>
Hello!!!!<b><%=request.getParameter("uname") %></b><br>
<table border="1">
<tr><th>Name</th><th>Values</th></tr>
<tr>
LIT/CSE/2024-25 26
220860131021 Advanced Java Programming (3160707)
<td>Request Method</td>
<td><%=request.getMethod() %></td>
</tr>
<tr>
<td>Request URL</td>
<td><%=request.getRequestURL() %></td>
</tr>
<tr>
<td>Request Protocol</td>
<td><%=request.getProtocol() %></td>
</tr>
<tr>
<td>Browser</td>
<td><%=request.getHeader("user-agent") %></td>
</tr>
</table>
<%
if(session.getAttribute("sessionVar")==null)
session.setAttribute("sessionVar",new Integer(0));
%>
<table>
<tr>
</tr>
<tr>
<td>
LIT/CSE/2024-25 27
220860131021 Advanced Java Programming (3160707)
</form>
</td>
</tr>
<tr><td></td></tr>
</table>
</body>
</html>
pageContext.jsp file:
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Page Context</title>
</head>
<body>
<%
if("Yes".equals(request.getParameter("other")))
{
pageContext.forward("/other");
%>
</body>
</html>
LIT/CSE/2024-25 28
220860131021 Advanced Java Programming (3160707)
Other.jsp file:
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Other JSP</title>
</head>
<body>
ServletConfig sc = getServletConfig();
count = Integer.parseInt(sc.getInitParameter("count"));
System.out.println("In jspInit");
%>
<%
%>
<%
this.log("log Message");
((HttpServlet)page).log("another message");
ServletContext ct = config.getServletContext();
out.println("Value of SessionVar
is:"+" <b>"+session.getAttribute("sessionVar")+"</b><br>");
LIT/CSE/2024-25 29
220860131021 Advanced Java Programming (3160707)
%>
</body>
</html>
Output:
LIT/CSE/2024-25 30
220860131021 Advanced Java Programming (3160707)
LIT/CSE/2024-25 31
220860131021 Advanced Java Programming (3160707)
Practical: 8
AIM: Write a program to create simple database application using
JSP.
Code:
getDetail.html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Detail Page</title>
</head>
<body>
<center>
<form action="Database.jsp">
<br>
</form>
</center>
</body>
</html>
Database.jsp file:
<%@ page import="java.sql.*" language="java" contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
LIT/CSE/2024-25 32
220860131021 Advanced Java Programming (3160707)
<meta charset="UTF-8">
<title>Employee Database</title>
</head>
<body>
<%
String id = request.getParameter("empid");
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(url,"system","system");
Statement st = con.createStatement();
%><center><h1><%out.println("Employee Details");
%></h1></center><br>
<%
out.println("<b>Employee
Id:</b>"+rs.getInt(1)+"<br><b>Employee
Name:</b>"+rs.getString(2)+"<br><b>Employee Salary:</b>"+rs.getInt(3));
}
catch(ClassNotFoundException e)
System.out.println(e);
catch(SQLException e1)
LIT/CSE/2024-25 33
220860131021 Advanced Java Programming (3160707)
System.out.println(e1);
%>
</body>
</html>
Output:
LIT/CSE/2024-25 34
220860131021 Advanced Java Programming (3160707)
Practical: 9
AIM: Write a program using java beans.
Code:
Login.jsp file:
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<br><br>
<tr><td colspan=2></td></tr>
<tr><td colspan=2></td></tr>
<tr>
<td><b>Login Name</b></td>
<td><input type="text" name="userName" value=""></td>
</tr>
<tr>
<td><b>Password</b></td>
</tr>
<tr>
LIT/CSE/2024-25 35
220860131021 Advanced Java Programming (3160707)
<td></td>
</tr>
<tr><td colspan=2></td></tr>
</table>
</form>
</body>
</html>
Welcome.jsp file:
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<br><br><br><br>
<%
if(session.getAttribute("username")!=null &&
session.getAttribute("username")!="")
%>
<%
LIT/CSE/2024-25 36
220860131021 Advanced Java Programming (3160707)
%>
</table>
</body>
</html>
Loginbean.jsp file:
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DatabaseSearch</title>
</head>
<body>
</jsp:useBean>
<%
session.setAttribute("username", db.getUserName());
session.setAttribute("password", db.getPassword());
response.sendRedirect("Welcome.jsp");
%>
</body>
</html>
LIT/CSE/2024-25 37
220860131021 Advanced Java Programming (3160707)
LoginBean.java file:
package logbean;
String userName="";
String password="";
return userName;
this.userName=userName;
return password;
this.password=password;
LIT/CSE/2024-25 38
220860131021 Advanced Java Programming (3160707)
Login.java file:
package logbean;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.sql.*;
@SuppressWarnings("deprecation")
res.setContentType("text/html");
PrintWriter out = res.getWriter();
PreparedStatement ps = null;
LIT/CSE/2024-25 39
220860131021 Advanced Java Programming (3160707)
ResultSet rs = null;
try
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url,userName,password);
if(req.getParameter("username")!=null &&
req.getParameter("username")!="" && req.getParameter("password")!=null &&
req.getParameter("password")!="")
username = req.getParameter("username").toString();
userpass = req.getParameter("password").toString();
System.out.println(strQuery);
ps = con.prepareStatement(strQuery);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
int count = 0;
while(rs.next())
session.setAttribute("username",rs.getString(2));
count++;
if(count>0)
res.sendRedirect("Welcome.jsp");
else
LIT/CSE/2024-25 40
220860131021 Advanced Java Programming (3160707)
res.sendRedirect("Login.jsp");
else
res.sendRedirect("Login.jsp");
}
System.out.println("Connected to the database");
con.close();
catch(Exception e)
e.printStackTrace();
Output:
LIT/CSE/2024-25 41
220860131021 Advanced Java Programming (3160707)
LIT/CSE/2024-25 42
220860131021 Advanced Java Programming (3160707)
Practical: 10
AIM: Write a Client Server program using TCP where client sends two
numbers and server responds with the sum of them.
Code:
Server.java file:
import java.io.*;
import java.net.*;
class Server
{
Socket s = ss.accept();
String result;
num1 = Double.parseDouble(br.readLine());
num2 = Double.parseDouble(br.readLine());
System.out.println("From Client:" + num2);
ps.print(result);
br.close();
ps.close();
LIT/CSE/2024-25 43
220860131021 Advanced Java Programming (3160707)
s.close();
ss.close();
Client.java file:
import java.net.*;
import java.io.*;
class Client
num2 = br1.readLine();
ps.println(num1);
ps.println(num2);
br.close();
br1.close();
LIT/CSE/2024-25 44
220860131021 Advanced Java Programming (3160707)
ps.close();
s.close();
Output:
Server side:
Client side:
LIT/CSE/2024-25 45
220860131021 Advanced Java Programming (3160707)
Practical: 11
AIM: Write a two-way chat application using swing and socket
programming.
Code:
ChatServer.java:
import java.io.*;
import java.util.*;
import java.net.*;
class ChatServer
{
System.out.println("Server Started....");
while(true)
LIT/CSE/2024-25 46
220860131021 Advanced Java Programming (3160707)
new ChatServer().process();
for(HandleClient c : clients)
if(! c.getUserName().equals(user))
{
c.sendMessage(user,message);
BufferedReader input;
PrintWriter output;
name = input.readLine();
users.add(name);
start();
LIT/CSE/2024-25 47
220860131021 Advanced Java Programming (3160707)
output.println(uname+":"+msg);
return name;
String line;
try
while(true)
line = input.readLine();
if(line.equals("end"))
clients.remove(this);
users.remove(name);
break;
ChatServer.this.broadcast(name,line);
}
}
catch(Exception ex)
System.out.println(ex.getMessage());
LIT/CSE/2024-25 48
220860131021 Advanced Java Programming (3160707)
ChatClient.java:
import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
String uname;
PrintWriter pw;
BufferedReader br;
JTextArea taMessages;
JTextField tfInput;
JButton btnSend,btnExit;
Socket client;
super(uname);
this.uname = uname;
pw = new PrintWriter(client.getOutputStream(),true);
LIT/CSE/2024-25 49
220860131021 Advanced Java Programming (3160707)
pw.println(uname);
buildInterface();
new MessagesThread().start();
taMessages.setRows(10);
taMessages.setColumns(50);
taMessages.setEditable(false);
JScrollPane sp = new
JScrollPane(taMessages,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScr
ollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(sp,"Center");
bp.add(tfInput);
bp.add(btnSend);
bp.add(btnExit);
add(bp,"South");
btnSend.addActionListener(this);
btnExit.addActionListener(this);
setSize(500,300);
setVisible(true);
pack();
}
LIT/CSE/2024-25 50
220860131021 Advanced Java Programming (3160707)
if(evt.getSource() == btnExit)
pw.println("end");
System.exit(0);
else
{
pw.println(tfInput.getText());
tfInput.setText("");
new ChatClient(name,servername);
catch(Exception ex)
System.out.println("Error -->"+ex.getMessage());
LIT/CSE/2024-25 51
220860131021 Advanced Java Programming (3160707)
String line;
try
while(true)
{
line = br.readLine();
taMessages.append(line+"\n");
catch(Exception ex)
Output:
Client 1:
LIT/CSE/2024-25 52
220860131021 Advanced Java Programming (3160707)
Client 2:
LIT/CSE/2024-25 53