Advanced Java Practicals
Advanced Java Practicals
No-23-188
PRACTICAL NO.1
1a.
AIM: Create a simple calculator application using servlet
[Link]
<html><head><title>Calculator App</title></head><body>
<form action="CalculatorServlet" >
Enter First Number <input type="text" name="txtN1" ><br>
Enter Second Number <input type="text" name="txtN2" ><br>
Select an Operation
double n1 = [Link]([Link]("txtN1"));
double n2 = [Link]([Link]("txtN2"));
double result =0;
String opr=[Link]("opr");
if([Link]("+")) result=n1+n2; if([Link]("-")) result=n1-n2;
if([Link]("*")) result=n1*n2; if([Link]("/")) result=n1/n2;
[Link]("<h1> Result = "+result); [Link]("</body></html>");
}}
OUTPUT:
1b.
AIM: Create a servlet for a login page. If the username and password are correct then it says
message “Hello ” else a message “login failed”
[Link]
<html><head><title>Login Form</title></head>
<form action="LoginServlet"
Enter User ID<input type="text" name="txtId"><br>
Enter Password<input type="password" name="txtPass"><br>
<input type="reset"><input type="submit" value=" Click to Login " ></form></html>
[Link]
package mypack;
import [Link].*;
import
[Link];
import [Link].*;
OUTPUT:
1c.
AIM: Create a registration servlet in Java using JDBC. Accept the details such as Username,
Password, Email, and Country from the user using HTML Form and store the registration
details in the database.
MySql queries
create database LoginDB;
use LoginDB;
create table user(username varchar(20) PRIMARY KEY, password varchar(20), email
varchar(20),
country varchar(20));
insert into user values ('admin','admin','admin@[Link]','India');
select * from user;
[Link]
<html><head><title>Registration Page</title></head>
<body>
<form action="RegisterServlet" >
<H1>Welcome to Registration page</H1>
Enter User Name <input type="text" name="txtUid"><br>
Enter Password <input type="password" name="txtPass"><br>
Enter Email <input type="text" name="txtEmail" ><br>
Enter Country <input type="text" name="txtCon" ><br>
<input type="reset" ><input type="submit" value="REGISTER" >
</form>
</body></html>
[Link]
package mypack;
import [Link].*;
import [Link].*;
import
[Link].*;
import [Link].*;
public class RegisterServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
String id = [Link]("txtUid");
String ps = [Link]("txtPass");
String em = [Link]("txtEmail");
String co = [Link]("txtCon")
try{ [Link]("[Link]");
Connection con
=[Link]("jdbc:mysql://localhost:3306/logindb","root","roo
t"); PreparedStatement pst = [Link]("insert into user
values(?,?,?,?)"); [Link](1,id);
[Link](2,ps);
[Link](3,em);
[Link](4,co);
PRACTICAL NO.2
2a.
AIM: Using Request Dispatcher Interface create a Servlet which will validate the password
entered by the user, if the user has entered "Servlet" as password, then he will be forwarded
to Welcome Servlet else the user will stay on the [Link] page and an error message will
be displayed.
[Link]
<html><head><title>Login Form</title></head>
<form action="LoginServlet" >
Enter User ID<input type="text" name="txtId"><br>
Enter Password<input type="password" name="txtPass"><br>
<input type="reset">
<input type="submit" value=" Click to Login " >
</form>
</html>
[Link]
package mypack;
import
[Link];
import [Link];
import
[Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
String uname = [Link]("txtId");
[Link]("<html><head>");
[Link]("<title>Servlet
LoginServlet</title></head>"); String uname =
[Link]("txtId");
OUTPUT:
2b.
AIM: Create a servlet that uses Cookies to store the number of times a user has visited a
servlet.
[Link]
<html>
<head><title>Cookie Demo</title></head>
<body>
<form action="Page1" >
Enter Your Name <input type="text" name="txtName"><br>
<input type="submit" value="~~~ Click to Enter ~~~">
</form>
</body>
</html>
[Link]
package mypack;
import
[Link];
import [Link];
import
[Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Page1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
[Link]("<html><head><title>Page1</title></head>");
[Link]("<body bgcolor=pink >");
String uname = [Link]("txtName");
Advanced Java 2025-2026
[Link].I.T Sem-V [Link]-23-188
else {
[Link](ck[i].getName()+ " = "+ck[i].getValue()); }
[Link]("<h1><a href=Page3 >Click to visit Page 3 </a></h1>");
else {
[Link]("</html>");
}}
}
[Link]
package mypack;
import [Link].*;
import
[Link];
import [Link].*;
public class Page4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
[Link]("<html><head><title>Page4</title></head>");
[Link]("<body bgcolor=yellow >");
Cookie [] ck = [Link]();
for(int i=0;i<[Link];i++){
if(ck[i].getName().equals("visit")){
int count =
[Link](ck[i].getValue())+1;
[Link]("<h1>Visit No : "+count+"</h1>");
ck[i] = new Cookie("visit",count+"");
[Link](ck[i]);
else {
[Link]("</html>");
}}
}
OUTPUT:
2c.
AIM: Create a servlet demonstrating the use of session creation and destruction. Also
check whether the user has visited this page first time or has visited earlier also using
sessions.
[Link]
<html>
<head><title>Session Demo</title></head>
<form action="Page1" method="get" >
Enter User ID <input type="text" name="txtName"><br>
<input type="reset" ><input type="submit" >
</form>
</html>
[Link]
package mypack;
import
[Link];
import [Link];
import
[Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Page1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
[Link]("<html><head><title>Servlet
Page1</title></head>"); HttpSession hs =
[Link](true);
if([Link]()){
[Link]("<body bgcolor=yellow>");
String name = [Link]("txtName");
[Link]("uname", name);
import [Link];
public class Page2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
import [Link];
import [Link];
public class LogoutServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
[Link]("<html><head><title>Servlet
LogoutServlet</title></head>");
[Link]("<body>");
[Link] hs = [Link]();
if(hs != null) [Link]();
[Link]("<h1>You are Logged out now </h1>");
[Link]("</body>");
[Link]("</html>");
}
}
OUTPUT:
PRACTICAL NO 3
3a.
Aim:Create a Servlet application to upload and download a file.
[Link]
<html>
<body>
<form action="FileUploadServlet" method="post" enctype="multipart/form-
data"> Select File to Upload:<input type="file" name="file" id="file">
Destination <input type="text" value="/tmp" name="destination">
<br>
<input type="submit" value="Upload file" name="upload" id="upload">
</form>
</body>
</html>
[Link]
package mypack;
import [Link].*;
import
[Link].*;
import
[Link];
import [Link].*;
@MultipartConfig
public class FileUploadServlet extends HttpServlet {
public void doPost(HttpServletRequest req,HttpServletResponse res) throws
ServletException,
IOException
{
[Link]("text/html");
PrintWriter out = [Link]();
String path=[Link]("destination");
Part filePart=[Link]("file");
Advanced Java 2025-2026
[Link].I.T Sem-V [Link]-23-188
String filename=[Link]().toString();
[Link]("<br><br><hr> file name: "+filename);
OutputStream os=null;
InputStream
is=null; try {
os=new FileOutputStream(new File(path+[Link]+filename));
is=[Link]();
int read=0;
[Link]
<html>
<head>
<title>Download files</title>
</head>
<body>
<h1>Downloading files</h1>
<a href="DownloadServlet2?filename=[Link]">document</a>
</body>
</html>
[Link]
package mypack;
import
[Link];
import
[Link];
import [Link];
import [Link];
import
[Link];
import [Link];
import [Link];
import [Link];
filename=[Link]("filename"); ServletContext
context=getServletContext();
InputStream is=[Link]("/"+filename);
// ServletOutputStream os=[Link]();
[Link]("Content-
Disposition","attachement;filename=\""+filename+"\"");
int i;
while((i=[Link]())!=-
1)
{
[Link](i);
}
[Link]();
}}}
Output:
3b.
Aim: Develop Simple Servlet Question Answer Application using Database.
SqlQuery
CREATE DATABASE qadb;
USE qadb;
CREATE TABLE quiz (
qno VARCHAR(5) PRIMARY KEY,
question VARCHAR(100),
op1 VARCHAR(50),
op2
VARCHAR(50),
op3
VARCHAR(50),
op4
VARCHAR(50),
ans VARCHAR(50)
);
INSERT INTO quiz VALUES
('001', 'What is the capital of India?', 'New Delhi', 'Kolkata', 'Chennai', 'Mumbai', 'New Delhi'),
('002', 'Who was the First President of India?', 'Dr. Rajendra Prasad', 'Dr. S. Radhakrishnan',
'Ram Nath Kovind', 'V. V. Giri', 'Dr. Rajendra Prasad'),
('003', 'What is ORM?', 'Object Ratio Mean', 'Object Rotation Measure', 'Object Relation
Mapping', 'Oracle Request Management', 'Object Relation Mapping'),
('004', 'Unit of Energy is ', 'Dozon', 'Kilo Meter', 'Joul', 'Hertz',
'Joul'),
('005', '--- is the smallest memory unit.', 'bit', 'byte', 'Kilo Byte', 'Giga Byte', 'bit');
[Link]
<html><head><title>Quiz Application</title></head>
<body>
<h1>Welcome to Quiz Servlet </h1>
<h1><a href="QuizServlet" >CLICK TO START QUIZ</a></h1>
</body>
Advanced Java 2025-2026
[Link].I.T Sem-V [Link]-23-188
</html>
[Link]
package mypack;
import [Link].*;
import [Link].*;
import
[Link].*;
import [Link].*;
int qno = 0;
while ([Link]()) {
qno++;
[Link]("<tr><td>" + [Link](1) + "</td>");
[Link]("<td>" + [Link](2) + "</td></tr>");
OUTPUT:
3c.
Aim:Create simple Servlet application to demonstrate Non-Blocking Read Operation
[Link]
<html>
<head>
<title>Non Blocking</title>
<meta charset="UTF-8">
<meta http-equiv="Refresh" content="0; URL=NonBlockingServlet5">
</head>
<body>
</body>
</html>
[Link]
package mypack;
import [Link].*;
import [Link].*;
import
[Link].*;
import [Link].*;
public class NonBlockingServlet5 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
[Link]("<h1>FileReader</h1>");
String filename="/WEB-
INF/[Link]"; ServletContext
c=getServletContext();
InputStream in=[Link](filename);
String
path="[Link]
h()+"/ReadingNonBloclingServlet5";
Advanced Java 2025-2026
[Link].I.T Sem-V [Link]-23-188
import [Link];
import
[Link];
public class ReadingListener5 implements ReadListener
{
private ServletInputStream input = null;
private AsyncContext ac = null;
ReadingListener5(ServletInputStream in, AsyncContext c) {
input = in;
ac = c;
}
@Override
public void onDataAvailable() throws IOException {
}
public void onAllDataRead() throws IOException {
[Link]();
}
public void onError(final Throwable t) {
[Link]();
[Link]();
}}
[Link]
package mypack;
import
[Link];
import [Link];
import [Link];
import [Link];
import
[Link];
import
[Link];
Advanced Java 2025-2026
[Link].I.T Sem-V [Link]-23-188
import [Link];
import [Link];
import [Link];
/**
*
* @author Beena
*/
@WebServlet (name = "ReadingNonBlockingServlet5", urlPatterns =
{"/ReadingNonBlockingServlet5"},asyncSupported = true )
public class ReadingNonBlockingServlet5 extends HttpServlet
{ @Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
AsyncContext ac = [Link]();
ServletInputStream in=[Link]();
[Link](new ReadingListener5(in,ac));
}}
[Link]
Harry Potter and the Philosopher's Stone
It Ends with Us
Pride and Prejudice Jane Austen.
1984 George Orwell.
The Great Gatsby F. Scott Fitzgerald.
Jane Eyre Charlotte Bronte
OUTPUT:
PRACTICAL NO 4
4a.
Aim:Develop a simple JSP application to display values obtained from the use of intrinsic
objects of various types.
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html><head><title>JSP Page</title></head>
<body>
<h1>Use of Intrinsic Objects in JSP</h1>
<h1>Request Object </h1>
Query String <%=[Link]() %><br>
Context Path <%=[Link]() %><br>
Remote Host <%=[Link]() %><br>
<h1>Response Object </h1>
Character Encoding Type <%=[Link]() %><br>
Content Type <%=[Link]() %><br>
Locale <%=[Link]() %><br>
<h1>Session Object </h1>
ID <%=[Link]() %><br>
Creation Time <%=new [Link]([Link]()) %><br>
Last Access Time<%=new [Link]([Link]()) %><br>
</body></html>
OUTPUT:
4b.
Aim:Develop a simple JSP application to pass values from one page to another with
validations. (Name-txt, age-txt, hobbies-checkbox, email-txt, gender-radio button).
[Link]
<html><head><title>User Information Page</title>
</head>
<body>
<form action="[Link]">
Enter Your Name<input type="text" name="name" ><br>
Enter Your Age<input type="text" name="age" ><br>
Select Hobbies
<input type="checkbox" name="hob" value="Singing">Singing
<input type="checkbox" name="hob" value="Reading">Reading Books
<input type="checkbox" name="hob" value="Football">Playing Football<br>
Enter E-mail<input type="text" name="email" ><br>
Select Gender
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="other">Other<br>
<input type="hidden" name="error" value="">
<input type="submit" value="Submit Form">
</form>
</body>
</html>
[Link]
package mypack;
public class CheckerBean {
private String name, age, hob, email, gender, error;
public CheckerBean(){error="";}
public void setName(String n){name=n;}
public void setAge(String a){age=a;}
{error+="<br>Age
Invalid";res=false;} return res;
}
}
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8" import="mypack.*" %>
<html><head><title>JSP Page</title></head>
<body>
<h1>Validation Page</h1>
<jsp:useBean id="obj" scope="request"
class="[Link]" >
<jsp:setProperty name="obj" property="*"/>
</jsp:useBean>
<%if ([Link]())
{ %>
<jsp:forward page="[Link]"/>
<% }
else {%>
<jsp:include page="[Link]"/>
<%}%>
<%=[Link]() %>
</body></html>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPEhtml>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>DATA VALIDATED SUCCESSFULLY</h1>
</body>
</html>
OUTPUT:
4c.
Aim: Create a registration and login JSP application to register and authenticate the user based
on username and password using JDBC.
[Link]
<html><head><title>New User Registration Page</title></head>
<body>
<form action="[Link]" >
<h1> New User Registration Page</h1>
Enter User Name <input type="text" name="txtName" ><br>
Enter Password <input type="password" name="txtPass1" ><br>
Connection con =
[Link]("jdbc:mysql://localhost:3306/rfid","root","root")
;
PreparedStatement stmt = [Link]("insert into user values (?,?,?,?)");
[Link](1, uname); [Link](2, pass1);
[Link](3, email); [Link](4, ctry);
int row = [Link]();
if(row==1) { [Link]("Registration Successful");
} else {
[Link]("Registration FFFFFAAAIIILLLL !!!!");
%><jsp:include page="[Link]" ></jsp:include>
<%
}
}catch(Exception e){[Link](e);}
}
else
{
[Link]("<h1>Password Mismatch</h1>");
%>
<jsp:include page="[Link]" ></jsp:include>
<% }
%>
</body>
</html>
[Link]
<html>
<body>
<h1>Login Page</h1>
<form action="[Link]" >
Enter User Name <input type="text" name="txtName" ><br>
Enter Password <input type="password" name="txtPass" ><br>
<input type="reset" ><input type="submit" value="~~~LOGIN~~" >
</form></body></html>
[Link]
<%@page contentType="text/html" import="[Link].*"%>
<html><body>
<h1>Registration JSP Page</h1>
<%
String uname=[Link]("txtName");
String pass = [Link]("txtPass");
try{
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/rfid","root","root")
;
PreparedStatement stmt = [Link]("select password from user where
username=?");
[Link](1, uname);
ResultSet rs = [Link]();
if([Link]()){
if([Link]([Link](1)))
{
[Link]("<h1>~~~ LOGIN SUCCESSFULLL ~~~ </h1>");
}
else{
[Link]("<h1> LOGIN FAILED !!!!!</h1>");
%>
<%
}
}
}catch(Exception e){[Link](e);}
%>
</body></html>
else{
[Link]("<h1> LOGIN FAILED !!!!!</h1>");
%>
<%
}
}
}catch(Exception e){[Link](e);}
%>
</body></html>
OUTPUT:
PRACTICAL NO 5
5a.
Aim:Create an html page with fields, eno, name, age, desg, salary. Now on submit this data to
a JSP page which will update the employee table of database with matching eno.
MySql queries
[Link]
<html>
<body>
<form action="[Link]" >
Enter Employee Number<input type="text" name="txtEno" ><br>
Enter Name<input type="text" name="txtName" ><br>
Enter age<input type="text" name="txtAge" ><br>
Enter Salary<input type="text" name="txtSal" ><br>
<input type="reset" ><input type="submit">
</form>
</body>
</html>
[Link]
<%@page contentType="text/html" import="[Link].*" %>
<html><body>
<h1>Employee Record Update</h1>
<%
String eno=[Link]("txtEno");
String name=[Link]("txtName");
String age = [Link]("txtAge");
String sal = [Link]("txtSal");
try{
[Link]("[Link]")
;
Connection con =
[Link]("jdbc:mysql://localhost:3306/black","root","root");
PreparedStatement stmt = [Link]("select * from emp where empid=?");
[Link](1, eno);
ResultSet rs = [Link]();
if([Link]()){
[Link]("<h1>~~~ Employee "+name+" Exist ~~~ </h1>");
PreparedStatement pst1= [Link]("update emp set salary=? where empid=?");
PreparedStatement pst2= [Link]("update emp set age=? where empid=?");
[Link](1, sal); [Link](2, eno);
OUTPUT:
5b.
Aim: Create a JSP page to demonstrate the use of Expression language.
[Link]
<body>
<h3>welcome to index page</h3>
<%
[Link]("user","Admin");
%>
<%
Cookie ck=new Cookie("name","mycookie");
[Link](ck);
%>
<form action="[Link]">
Enter Name:<input type="text" name="name" /><br/><br/>
<input type="submit" value="Submit"/>
</form>
<form action="[Link]" method="get">
<input type="submit" value="Arithemetic Operator"/>
</form>
<form action="[Link]" method="get">
<input type="submit" value="Logical Operator"/>
</form>
<form action="[Link]" method="get">
<input type="submit" value="Relational Operator"/>
</form>
<form action="ELconditional [Link]" method="get">
<input type="submit" value="Conditional Operator"/>
</form>
<form action="Empty [Link]" method="get">
<input type="submit" value="Empty Operator"/>
</form>
</body>
[Link]
<body>
Welcome, ${ [Link] }
Session Value is ${ [Link] }
Cookie name is , ${[Link]}
</body>
[Link]
<body>
<h2> Arithemetic Operator</h2>
<%-- arithmetic op --%>
5*5+4: ${5*5+4} <br>
1.4E4+1.4: ${1.4E4+1.4}<br>
10 mod 4: ${10 mod 4}<br>
15 div 3: ${15 div 3}<br>
</body>
[Link]
<body>
<%-- LogicalOperator --%>
<h2>Logical Operator</h2>
true and true: ${true and true}<br>
true && false: ${true && false}<br>
${10.0==10} <br>
10.0 eq 10: ${10.0 eq 10} <br>
((20*10)!= 200): ${((20*10)!= 200)} <br>
3 ne 3: ${3 ne 3} <br>
3.2>=2: ${3.2>=2} <br>
3.2 ge 2: ${3.2 ge 2} <br>
2<3: ${2<3} <br>
4 lt 6: ${4 lt 6} <br>
2 <= 4: ${2 <= 4} <br>
4 le 2: ${4 le 2}
</body>
ELconditional [Link]
<body>
<h2>Conditional Operator</h2>
The result of 10>2 is: "${(10>1)?'greater':'lesser'}"
</body>
Empty [Link]
<body>
<H1>Empty Operator Example</H1>
The Value for the Empty operator is:: ${empty "txxt"}
</body>
OUTPUT:
5c.
Aim: Create a JSP application to demonstrate the use of JSTL. Basic insert, update and
delete example using core and sql tag libraries in JSTL.
MySql queries
[Link]
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Choose Option</h1>
<a href="[Link]">Insert Record</a>
<p></p>
<a href="[Link]">Display Record</a>
</body></html>
[Link]
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<%@ taglib uri="[Link] prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="[Link]" method="post">
<table border="0" cellspacing="2" cellpadding="5">
<thead>
<tr>
<th colspan="2">Purchase Product</th>
</tr>
</thead>
<tbody>
<tr>
<td><label>Product Name</label></td>
<td><input type="text" name="pname" /></td>
</tr>
<tr>
<td><label>Quantity</label></td>
<td><input type="text" name="qty" /></td>
</tr>
<tr>
<td><input type="submit" value="Save" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</tbody>
</table>
</form>
<!-- Display Error Message if exists -->
<font color="red">
<c:if test="${not empty [Link]}">
<c:out value="${[Link]}" /><br>
<a href="[Link]">Go Back</a>
</c:if>
</font>
<!-- Display Success Message if exists -->
<font color="green">
<c:if test="${not empty [Link]}">
<c:out value="${[Link]}" /><br>
<a href="[Link]">Go Back</a>
</c:if>
</font></body></html>
[Link]
<%@ page import="[Link].*, [Link].*, [Link].*" %>
<%@ taglib uri="[Link] prefix="c" %>
<%@ taglib uri="[Link] prefix="sql" %>
<html>
<head>
<title>INSERT Operation</title>
</head>
<body>
<!-- Validate input parameters -->
<c:if test="${empty [Link] or empty [Link]}">
<c:redirect url="[Link]">
<c:param name="errMsg" value="Please Enter Product and Quantity" />
</c:redirect>
</c:if>
</script>
</head>
<body>
<sql:setDataSource var="dbsource"
driver="[Link]"
url="jdbc:mysql://localhost/payal1
" user="root"
password="root" />
<sql:query dataSource="${dbsource}" var="result">
SELECT * FROM product;
</sql:query>
<center>
<form>
<table border="1" width="40%">
<caption><strong>Product List</strong></caption>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Quantity</th>
<th colspan="2">Action</th>
</tr>
<c:forEach var="row" items="${[Link]}">
<tr>
<td><c:out value="${[Link]}" /></td>
<td><c:out value="${[Link]}" /></td>
<td><c:out value="${[Link]}" /></td>
<td>
<a href="[Link]?id=${[Link]}">Update</a>
</td>
<td>
<a href="javascript:confirmGo('Sure to delete this record?',
'[Link]?id=${[Link]}')">Delete</a>
</td>
</tr>
</c:forEach>
</table>
</form>
<br>
<a href="[Link]">Go Home</a>
</center></body></html
>
[Link]
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<%@ page import="[Link].*, [Link].*, [Link].*" %>
<%@ taglib uri="[Link] prefix="c" %>
<%@ taglib uri="[Link] prefix="sql" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Update Product</title>
</head>
<body>
</body></html>
[Link]
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<%@ page import="[Link].*, [Link].*, [Link].*" %>
</p>
PRACTICAL NO 6
6a.
Aim: Create a Currency Converter application using EJB.
[Link]
<html><head><title>Currency Converter</title></head>
<body>
<form action="CCServlet" >
Enter Amount <input type="text" name="amt"><br>
Select Conversion Type
if([Link]("type").equals("r2d"))
{
[Link]("<h1>"+amt+ " Rupees = "+obj.r2Dollor(amt)+" Dollors</h1>");
}
if([Link]("type").equals("d2r"))
{
[Link]("<h1>"+amt+ " Dollors = "+obj.d2Rupees(amt)+" Rupees</h1>");
}
}}
[Link]
package mybeans;
import [Link];
@Stateless
public class CCBean {
public CCBean(){}
public double r2Dollor(double r){ return r/65.65; }
public double d2Rupees(double d){ return d*65.65; }
}
OUTPUT:
6b.
Aim:Develop a Simple Room Reservation System Application Using EJB.
MySql queries
Create table roombook(RoomId varchar(4) PRIMARY KEY, RoomType varchar(20), charges
Decimal(7,2), cust varchar(20), mob varchar(20) , status varchar(10));
insert into roombook values('1001','Delux',5000.00,'','','Not Booked');
insert into roombook values('1002','Super Delux',7000.00,'','','Not Booked');
insert into roombook values('1003','Suit',9500.00,'','','Not Booked');
insert into roombook values('2001','Delux',5000.00,'','','Not Booked');
insert into roombook values('2002','Super Delux',7000.00,'','','Not Booked');
insert into roombook values('2003','Suit',9500.00,'','','Not Booked');
[Link]
<form action="RBServlet"
> Select a room Type
<input type="radio" name="txtType" value="Delux">Delux
<input type="radio" name="txtType" value="Super Delux">Super Delux
<input type="radio" name="txtType"
value="Suit">Suit<br> Enter Your Name<input
type="text" name="txtCust" ><br> Enter Mobile
No.<input type="text" name="txtMob" ><br>
<input type="reset" ><input type="submit" value="Book Room">
</form>
[Link]
package mypack;
import [Link].*;
import
[Link].*;
import
[Link].*;
import [Link];
import [Link];
public class RBServlet extends HttpServlet {
import [Link];
import [Link].*;
@Stateless
public RRBean(){}
public String
roomBook1(String rt, String
cn, String cm){
String msg="";
try{
[Link]("[Link]
[Link]");
Connection con =
[Link](
"jdbc:mysql://localhost:3306/pi
Advanced Java 2025-2026
[Link].I.T Sem-V [Link]-23-188
nk?useSSL=false&setTimezon
e=UTC","root","root");
PreparedStatement pst =
[Link](query);
[Link](1,rt);
ResultSet rs=
[Link]();
if([Link]()){
String rno=[Link](1);
PreparedStatement stm1 =
[Link]("update
roombook1 set cust=? where
RoomId=? ");
PreparedStatement stm2 =
[Link]("update
roombook1 set mob=? where
RoomId=? ");
PreparedStatement stm3 =
[Link]("update
roombook1 set status=? where
RoomId=? ");
[Link](1,cn);
[Link](2,rno);
[Link](1,cm);
[Link](2,rno);
[Link](1, "Booked");
[Link](2,rno);
[Link]();
[Link]();
[Link]();
else
}catch(Exception
e){msg=""+e;}
return msg;}
OUTPUT:
6c.
Aim:Develop simple shopping cart application using EJB [Stateful Session Bean].
[Link]
package ejb;
import [Link].*;
import [Link].*;
import
[Link].*;
@Stateful
public class ShoppingCart
{ List<String> contents;
String customerName;
private Connection conn = null;
private ResultSet rs;
private Statement stmt = null;
private String query = null;
public void initialize(String person)
{ if (person != null) {
customerName = person;
try {
[Link]("[Link]").newInstance();
conn = [Link]("jdbc:mysql://localhost:3306/dbmart", "root","root");
} catch(ClassNotFoundException | IllegalAccessException | InstantiationException |
SQLException e) {
stmt = [Link]();
query = "INSERT INTO cart VALUES('" + customerName + "','" + title + "')";
[Link](query);
} catch(SQLException e) {
[Link]("Sorry failed to insert values from the database table. " +
[Link]());
}
}
public void removeBook(String title)
{ try {
stmt = [Link]();
query = "DELETE FROM cart WHERE UserName='" + customerName + "' AND
ItemName='" + title + "'";
[Link](query);
} catch(SQLException e) {
[Link]("Sorry failed to delete values from the database table. " +
[Link]());
}
}
public List<String> getContents()
{ try {
stmt = [Link]();
query = "SELECT * FROM cart WHERE UserName='" + customerName +
"'"; rs = [Link](query);
while([Link]()) {
[Link]([Link]("ItemName"));
}
} catch(SQLException e) {
[Link]("Sorry failed to select values from the database table. " +
[Link]());
}
return contents;
}
@Remove()
public void remove() {
contents = null;
}
}
[Link]
<%@page import="[Link], [Link], [Link],
[Link]"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%!
private static ShoppingCart cart;
public void jspInit() {
try {
InitialContext ic = new InitialContext();
cart = (ShoppingCart) [Link]("java:global/practical6c1/ShopingCart");
} catch (Exception ex) {
[Link]("Could not create cart bean." + [Link]());
}
}
%>
<%
if([Link]("txtCustomerName") != null) {
[Link]([Link]("txtCustomerName"));
} else {
[Link]("Guest");
}
if ([Link]("btnRmvBook") != null) {
String books[] = [Link]("chkBook");
if (books != null) {
for (int i=0; i<[Link]; i++) {
[Link](books[i]);
}
}
}
if ([Link]("btnAddBook") != null) {
String books[] = [Link]("chkBook");
if (books != null) {
for (int i=0; i<[Link]; i++) {
[Link](books[i]);
}
}
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset=UTF-8">
<title>Shopping Cart</title>
</head>
<body style="background-color: pink;">
<h1 style="text-align: center;">Books For Sale</h1><br>
<form method="post">
Customer Name: <input type="text" name="txtCustomerName" value=<%=
[Link]("txtCustomerName")%> /><br>
<b>Book Titles</b><br>
<input type="checkbox" name="chkBook" value="Struts 2.0 For Beginners">Struts 2.0 For
Beginners<br>
<input type="checkbox" name="chkBook" value="Oracle 11g For Professionals">Oracle 11g
For Professionals<br>
<input type="checkbox" name="chkBook" value="Hibernate 3 For Beginners">Hibernate 3
For Beginners<br>
<input type="checkbox" name="chkBook" value="Java Persistence API In EJB 3 For
Beginners">Java Persistence API In EJB 3 For Beginners<br>
<br>
while ([Link]())
{
String title = (String) [Link]();
%>
<%= title %><br>
<%
}
}
%>
</form>
</body>
</html>
MySQL queris
CREATE DATABASE cartdb;
USE cartdb;
CREATE TABLE cart (
UserName VARCHAR(35),
ItemName VARCHAR(50)
);
OUTPUT:
PRACTICAL NO 7
7a.
Aim:Develop simple EJB application to demonstrate Servlet Hit count using Singleton
Session Beans.
[Link]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
</html>
CountServletHitsBean
package ejb;
import [Link];
@Singleton
return hitCount++;
ServletClient
package servlet;
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link].*;
@Override
IOException
[Link]("text/html");
PrintWriter out=[Link]();
Output:
7b.
Aim:Develop simple visitor Statistics application using Message Driven Bean
[Link]
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<!DOCTYPE html>
<%!
Connection connection=null;
Session mySession=null;
MessageProducer messageProducer=null;
TextMessage message=null;
%>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
try{
queue= (Queue)[Link]("jms/Queue");
connectionFactory=(ConnectionFactory)[Link]("jms/QueueFactory");
connection= [Link]();
mySession=[Link](false, Session.AUTO_ACKNOWLEDGE);
messageProducer=[Link](queue);
message=[Link]();
[Link]([Link]());
[Link](message);
catch(JMSException e)
%>
</body>
</html>
BasicMessageBean
package ejb;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@MessageDriven(activationConfig = {
"[Link]")
})
@Resource
public BasicMessageBean() {
@Override
try {
[Link]([Link]());
catch (JMSException e) {
[Link]();
VisitorStatBean
package ejb;
import [Link].*;
Advanced Java 2025-2026
[Link].I.T Sem-V [Link]-23-188
import [Link];
import [Link];
import [Link];
@Stateless
@PostConstruct
try {
[Link]("[Link]").newInstance();
conn=[Link]("jdbc:mysql://localhost/vani?useSSL=false&setTimezo
ne=UTC", "root", "root");
catch (Exception e) {
[Link]([Link]());
@PreDestroy
try {
[Link]();
} catch (Exception e) {
[Link]([Link]());
try {
st= [Link]();
[Link](query);
catch (SQLException e)
try {
st=[Link]();
[Link](query);
[Link]("Cannot Update"+[Link]());
SQL Query:
Firstvisitdt TIMESTAMP,
Visits INT
Advanced Java 2025-2026
[Link].I.T Sem-V [Link]-23-188
);
Localhost:4848,
Find Resources -> connectors -> Connector Resources double click on Connector
Resources -> click on ‘New’ Button -> write JNDI name as -> jms/QueueFactory.
Find Admin Object Resources and double click on that -> click on ‘New’ Button ->
Output:
7c.
using EJB
[Link]
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<!DOCTYPE html>
<%!
try
obj=(MarksEntryBean)[Link]("java:global/practical7c/MarksEntryBean");
catch(Exception e)
%>
<%
if([Link]("InsertMarks")!=null)
String sname;
sname = [Link]("sname");
marks1=[Link]([Link]("m1"));
marks2=[Link]([Link]("m2"));
marks3=[Link]([Link]("m3"));
[Link](sname,marks1,marks2,marks3);
%>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h2>Enter Details</h2>
</form>
</body>
</html>
MarksEntryBean
package ejb;
import [Link].*;
import [Link];
@Stateful
String sname;
int m1,m2,m3;
Connection con=null;
Statement st=null;
String query="";
try
[Link]("[Link]");
con=[Link]("jdbc:mysql://localhost:3306/marksdb?useSSL=false&s
etTimezone=UTC", "root","root");
st=[Link]();
[Link](query);
catch(Exception e){[Link](e);}
Output:
PRACTICAL NO 8
Aim: Develop a Guestbook Application Using JPA
[Link]
<!DOCTYPE html>
<html>
</form>
</body>
</html>
GuestBookView
<%@page import="[Link]"%>
<!DOCTYPE html>
<%!
%>
<%
try {
InitialContext().lookup("java:comp/env/persistence/EntityManager");
try {
[Link](guest);
[Link](message);
[Link](messageDate);
[Link]().begin();
[Link](gb);
[Link]().commit();
[Link]("[Link]");
} catch (Exception e) {
if ([Link]().isActive()) {
[Link]().rollback();
[Link]();
} catch (Exception e) {
[Link]();
%>
<html>
<body>
View the Guest Book <b>Click <a href="[Link]"> here</a> to sign the guestbook.</b>
<hr />
<%
if (guestbook != null) {
%>
<%
%>
</body>
</html>
Advanced Java 2025-2026
[Link].I.T Sem-V [Link]-23-188
[Link]
package asif;
import [Link].*;
@Entity
@Table(name="GuestBook")
@Id
@GeneratedValue(strategy = [Link])
@Column(name="VisitorName")
@Column(name="Message")
@Column(name="MessageDate")
public GuestBook() {
return visitorNo;
[Link] = visitorNo;
return visitorName;
[Link] = visitorName;
return message;
[Link] = message;
return messageDate;
[Link] = messageDate;
Output: