Java Practicals
Java Practicals
CERTIFICATE
This is to certify that Miss: TISHA DINESH TELI and Roll No: 8552 of TY [Link]
I.T. Semester 5 has completed the PRACTICAL FILE in the subject
ENTERPRISE JAVA during the academic year 2022-23 under the guidance
of PROF. OMKAR SHERKHANE being the partial requirement for the
fulfillment of the curriculum of Degree of Bachelor of Science in
Information Technology, University Of Mumbai.
Place: Panvel
Date:
Name & Signature of Faculty:
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
Index
Practical Details Date
No.
1 Implement the following Simple Servlet applications.
a. Create a simple calculator application using
servlet.
b. Create a servlet for a login page. If the
username and password are correct then it
says message “Hello <username>” else a
message “login failed”.
c. 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.
2 Implement the following Servlet applications with
Cookies and Sessions.
a. 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.
b. Create a servlet that uses Cookies to store the
number of times a user has visited servlet.
c. 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.
3 Implement the Servlet IO and File applications.
a. Create a Servlet application to upload and
download a file.
b. Develop Simple Servlet Question Answer
Application using Database.
c. Create simple Servlet application to
demonstrate Non-Blocking Read Operation.
4 Implement the following JSP applications.
a. Develop a simple JSP application to display
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
values obtained from the use of intrinsic
objects of various types.
b. 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).
c. Create a registration and login JSP application
to register and authenticate the user based on
username and password using JDBC.
5 Implement the following JSP JSTL and EL Applications.
a. 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.
b. Create a JSP page to demonstrate the use of
Expression language.
c. Create a JSP application to demonstrate the
use of JSTL.
6 Implement the following EJB Applications.
a. Create a Currency Converter application using
EJB.
b. Develop a Simple Room Reservation System
Application Using EJB.
c. Develop simple application which work with
Interceptors & JNDI.
7 Implement the following EJB applications with
different types of Beans.
a. Develop simple EJB application to demonstrate
Servlet Hit count using Singleton Session Beans.
b. Develop simple visitor Statistics application
using Message Driven Bean [Stateless Session
Bean].
c. Develop simple Marks Entry Application to
demonstrate accessing Database using EJB
8 Implement the following JPA applications.
a. Develop a simple Inventory Application Using
JPA.
b. Develop a Guestbook Application Using JPA.
c. Create simple JPA application to store and
retrieve Book details
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
9 Implement the following JPA applications with ORM
and Hibernate.
a. Develop a JPA Application to demonstrate use
of ORM associations.
b. Develop a Hibernate application to store
Feedback of Website Visitor in MySQL
Database.
c. Develop a Hibernate application to store and
retrieve employee details in MySQL Database.
10 Implement the following Hibernate applications.
a. Develop an application to demonstrate
Hibernate One-To-One Mapping Using
Annotation.
b. Develop Hibernate application to enter and
retrieve course details with ORM Mapping.
c. Develop a five-page web application site using
any two or three Java EE Technologies
Practical No:- 1
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
Aim:- Implement the following Simple Servlet applications.
a. Create a simple calculator application using servlet.
Code:-
[Link]
<html>
<head>
<title>Calculator</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="Calculator_H" method="Post">
Enter 1st Number: <input type="number" name="t1"><br>
Enter 2nd Number: <input type="number" name="t2"><br>
<input type="submit" name="add" value="+">
<input type="submit" name="sub" value="-">
<input type="submit" name="mul" value="*">
<input type="submit" name="div" value="/">
</form>
</body>
</html>
Calculator_H.java
import [Link].*;
import [Link].*;
import [Link].*;
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
import [Link].*;
@WebServlet(urlPatterns = {"/Calculator_H"})
if([Link]("add")!=null)
{
int c=a+b;
[Link]("Addition:- "+c);
}
if([Link]("sub")!=null)
{
int d=a-b;
[Link]("Subtraction:- "+d);
}
if([Link]("mul")!=null)
{
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
int e=a*b;
[Link]("Multiplication:- "+e);
}
if([Link]("div")!=null)
{
int f=a/b;
[Link]("Division:- "+f);
}
}
catch(Exception e)
{
[Link]("Error: "+e);
}
}
}
Output:-
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
b. Create a servlet for a login page. If the username and password are correct
then it says the message “Hello <username>” else a message “login failed”.
Code:-
[Link]
<html>
<head>
<title>Login Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Login Form</h1>
<form action="Login_Forms" method="Post">
Username: <input type="text" name="t1"> <br>
Password: <input type="password" name="t2"> <br>
<input type="submit" name="s1" value="Login">
</form>
</body>
</html>
Login_Forms.java
import [Link].*;
import [Link];
import [Link];
import [Link].*;
@WebServlet(urlPatterns = {"/Login_Forms"})
public class Login_Forms extends HttpServlet
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException
{
[Link]("text/html");
PrintWriter out=[Link]();
try
{
String u=[Link]("t1");
String p=[Link]("t2");
if([Link]("Admin")&&[Link]("123"))
{
[Link]("Hello "+u);
}
else
{
[Link]("Login Failed");
}
}
catch(Exception e)
{
[Link]("Error: "+e);
}
}
}
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
Output:-
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
c. 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.
Code:-
[Link]
<html>
<head>
<title>Registration Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Registration Page</h1>
<form action="Registered_Form" method="Post">
Enter Username: <input type="text" name="t1">
<br>
Enter Password: <input type="password" name="t2">
<br>
Enter Email ID: <input type="email" name="t3">
<br>
Enter Country: <input type="text" name="t4">
<br>
<input type="submit" name="s1" value="Register">
</form>
</body>
</html>
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
Registered_Form.java
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link].*;
@WebServlet(urlPatterns = {"/Registered_Form"})
[Link]("text/html");
PrintWriter out=[Link]();
String u=[Link]("t1");
String p=[Link]("t2");
String em=[Link]("t3");
String co=[Link]("t4");
try
{
[Link]("[Link]");
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
Connection
c=[Link]("jdbc:derby://localhost:1527/Login_Credentials","a
pp","app");
Statement st=[Link]();
if([Link]("s1")!=null)
{
int i=[Link]("insert into Login_Table
values('"+u+"','"+p+"','"+em+"','"+co+"')");
if(i>0)
{
[Link]("Data Saved");
}
else
{
[Link]("Registration Failed");
}
}
[Link]();
[Link]();
}
catch(Exception e)
{
[Link]("Error: "+e);
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
}
}
}
Output:-
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
Practical No:- 2
Aim:- Implement the following Servlet applications with Cookies and Sessions.
a. 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.
Code:-
[Link]
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
<html>
<head>
<title>Login Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Login Form</h1>
<form action="Login_Form" method="Post">
Username: <input type="text" name="t1">
<br>
Password: <input type="password" name="t2">
<br>
<input type="submit" name="s1" value="Login">
</form>
</body>
</html>
Login_Form.java
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
@WebServlet(urlPatterns = {"/Login_Form"})
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
public class Login_Form extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException
{
[Link]("text/html");
PrintWriter out=[Link]();
try
{
String u=[Link]("t1");
String p=[Link]("t2");
if([Link]("User")&&[Link]("Servlet"))
{
RequestDispatcher rd=[Link]("Servlet_Page");
[Link](request,response);
}
else
{
[Link]("Invaild Username or Password");
}
}
catch(Exception e)
{
[Link]("Error: "+e);
}
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
}
}
Servlet_Page.java
import [Link].*;
import [Link];
import [Link];
import [Link].*;
@WebServlet(urlPatterns = {"/Servlet_Page"})
public class Servlet_Page extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException
{
[Link]("text/html");
PrintWriter out=[Link]();
try
{
[Link]("Welcome Servlet");
}
catch(Exception e)
{
[Link]("Error: "+e);
}
}
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
}
Output:-
b. Create a servlet that uses Cookies to store the number of times a user has
visited servlet.
Code:-
Cookie_Times.java
import [Link].*;
import [Link];
import [Link];
import [Link].*;
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
@WebServlet(urlPatterns = {"/Cookie_Times"})
public class Cookie_Times extends HttpServlet
{
int c=1;
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]())
{
Cookie c1 = new Cookie("Count",""+c);
c=[Link]([Link]());
[Link]("Visited "+c);
c++;
c1=new Cookie("Count",""+c);
}
}
Output:-
Session_Times.java
import [Link].*;
import [Link];
import [Link];
import [Link].*;
@WebServlet(urlPatterns = {"/Session_Times"})
public class Session_Times extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]())
{
HttpSession hs = [Link](true);
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
if([Link]())
{
String name = [Link]("t1");
[Link]("uname", name);
[Link]("visit", "1");
[Link]("Welcome to your first visit<br>");
}
else
{
[Link]("Welcome Again<br> ");
int visit = [Link]((String)[Link]("visit"))+1;
[Link]("You Visited "+visit+" Times<br>");
[Link]("visit", ""+visit);
}
[Link]("Your Session ID "+[Link]()+"<br>");
if([Link]("b2")!=null)
{
[Link]();
[Link]("You are Logged out now<br>");
}
}
}
}
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
Output:-
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
Practical No:- 3
Aim:- Implement the Servlet IO and File applications.
a. Create a Servlet application to upload and download a file.
Code 1:- File Upload
[Link]
<html>
<head>
<title>File Upload</title>
<meta charset="UTF-8">
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>File Upload Application</h1>
<form action="FileUploadServlet" enctype="multipart/form-data" method="POST">
File:<input type="file" name="file" id="file"/><br/><br/>
Destination:<input type="text" value="" name="destination"/><br/><br/>
<input type="submit" value="UploadFile" name="upload" id="upload"/>
</form>
</body>
</html>
[Link]
package Upload;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
@WebServlet(name="FileUploadServlet",urlPatterns={"/FileUploadServlet"})
@MultipartConfig
public class FileUploadServlet extends HttpServlet
{
try
{
out=new FileOutputStream(new File(path+[Link]+fileName));
filecontent=[Link]();
int read=0;
final byte[] bytes=new byte[1024];
while((read=[Link](bytes))!=-1)
{
[Link](bytes,0,read);
}
[Link]("FileUploaded.");
}
catch(Exception e)
{
[Link](e);
}
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
finally
{
if(out!=null)
[Link]();
if(filecontent!=null)
[Link]();
if(writer!=null)
[Link]();
}
}
}
}
return null;
}
@Override
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
processRequest(request,response);
}
@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
processRequest(request,response);
}
}
Output:-
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
[Link]
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
@WebServlet(urlPatterns = {"/DownloadServlet"})
public class DownloadServlet extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
[Link]("text/html;charset=UTF-8");
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
try (PrintWriter out = [Link]())
{
[Link]("APPLICATION/OCTET-STREAM");
String filename=[Link]("filename");
ServletContext context = getServletContext();
InputStream is = [Link]("/" + filename);
[Link]("Content-Disposition","attachment; filename=\"" +
filename + "\"");
int i;
while ((i=[Link]()) != -1)
{
[Link](i);
}
[Link]();
[Link]();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
throws ServletException, IOException
{
processRequest(request, response);
}
}
Output:-
Output:-
Output:-
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
Practical No:- 4
Aim:- Implement the following JSP applications.
a. Develop a simple JSP application to display values obtained from the use of
intrinsic objects of various types.
Code:-
[Link]
<html>
<head>
<title>Calculate</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="[Link]" method="post">
Enter First Number: <input type="number" name="t1"><br>
Enter Second Number: <input type="number" name="t2"><br><br>
<input type="submit" name="a1" value="Add">
<input type="submit" name="s1" value="Subtract">
<input type="submit" name="m1" value="Multiply">
<input type="submit" name="d1" value="Divide">
</form>
</body>
</html>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Calculate</title>
</head>
<body>
<%
try
{
int i = [Link]([Link]("t1"));
int j = [Link]([Link]("t2"));
if([Link]("a1")!=null)
{
int k = add(i,j);
[Link]("Addition: "+k);
}
if([Link]("s1")!=null)
{
int k = sub(i,j);
[Link]("Subtraction: "+k);
}
if([Link]("m1")!=null)
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
{
int k = mul(i,j);
[Link]("Multiplication: "+k);
}
if([Link]("d1")!=null)
{
float k = div(i,j);
[Link]("Division: "+k);
}
}
catch(Exception e)
{
[Link]("Error: "+e);
}
%>
<%!
public int add(int x,int y)
{
return(x+y);
}
[Link]("Name: "+name);
[Link]("<br>Age: "+age);
[Link]("<br>Hobbies: ");
for (int i = 0; i < [Link]; i++)
{
[Link](hobby[i] + ", ");
}
[Link]("<br>Email: "+email);
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
[Link]("<br>Gender: "+gender);
}
catch(Exception e)
{
[Link]("Error: "+e);
}
%>
</body>
</html>
Output:-
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
c. Create a registration and login JSP application to register and authenticate the
user based on username and password using JDBC.
Code:-
[Link]
<html>
<head>
<title>REGISTRATION</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="[Link]" method="POST">
name: <input type="text" name="t1"><br>
username(email-id): <input type="email" name="t2"><br>
password: <input type="password" name="t3"><br>
re-enter password: <input type="password" name="t4"><br>
<input type="submit" value="register" name="b1">
</form>
</body>
</html>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8" import="[Link].*,
[Link].*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
</head>
<body>
<%
try
{
String name=[Link]("t1");
String username=[Link]("t2");
String pass=[Link]("t3");
String reenter=[Link]("t4");
if([Link](reenter))
{
[Link]("[Link]");
Connection c =
[Link]("jdbc:derby://localhost:1527/practical4c","app","app"
);
PreparedStatement ps=[Link]("insert into login values(?,?,?)");
[Link](1,name);
[Link](2,username);
[Link](3,pass);
int i=[Link]();
if(i>0)
{
[Link]("data saved");
}
else
{
[Link]("data not saved");
}
}
else
{
[Link]("password does not match");
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
}
catch(Exception e)
{
[Link]("Error="+e);
}
%>
</body>
</html>
[Link]
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="[Link]" method="POST">
username(email-id): <input type="email" name="t1"><br>
password: <input type="password" name="t2"><br>
<input type="submit" value="login" name="b1">
</form>
</body>
</html>
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"
import="[Link].*,[Link].*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
try
{
String username=[Link]("t1");
String pass=[Link]("t2");
[Link]("[Link]");
Connection
c=[Link]("jdbc:derby://localhost:1527/practical4c","app","ap
p");
PreparedStatement ps=[Link]("select * from login where
username=? and pass=? ");
[Link](1,username);
[Link](2,pass);
ResultSet rs=[Link]();
if([Link]())
{
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
RequestDispatcher rd=[Link]("[Link]");
[Link](request, response);
}
else
{
[Link]("invalid user");
}
}
catch(Exception e)
{
[Link](e);
}
%>
</body>
</html>
[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>
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C
</head>
<body>
<%
String u=[Link]("t1");
[Link]("welcome " +u);
%>
</body>
</html>
Output:-
Name:- Teli Tisha Dinesh Roll No:- 8552
Class:- T.Y. IT Division:- C