0% found this document useful (0 votes)
4 views

Java Document by Harsh-1

Doc

Uploaded by

monikarnayak4444
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Document by Harsh-1

Doc

Uploaded by

monikarnayak4444
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 105

ADVANCED JAVA TECHNOLOGIES

T.Y.B.Sc.IT (INFORMATION TECHNOLOGY)

BY

Name: Belekar Harsh Anil

Seat Number:

N. B. MEHTA (VALWADA) SCIENCE COLLEGE, BORDI


BORDI - 401 701
MAHARASHTRA

DEPARTMENT OF INFORMATION TECHNOLOGY


T.Y.B.Sc.IT (INFORMATION TECHNOLOGY)
Semester-5

Academic year 2024-2025

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 1


TABLE OF CONTENT
Sr.n Practicals Signature
o
1. Implement the following Simple Servlet applications.
Create a servlet for a login page. If the username and
A. password are correct then it says message “Hello” else a
message “login failed”.
B. 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.
C. Write a servlet code to store the biodata details of a
user into the database. (Name, age, address, hobbies,
gender, Qualification).
D. Write a Java Program using Servlet to authenticate
user’s username and password.
E. Develop servlet application for accepting user
registration details and displaying it on another page (At
least 6 Values).
F. Develop servlet application of basic calculator.
G. Developed a servlet application to demonstrate simple
validation. And If user left any filed empty [Username or
Password] then user will stay on the index.html page
and an error message will be displayed.
H. Create a servlet which authenticate with Database.
2. Implement the following Servlet applications with
Cookies and Sessions.
Create a servlet that uses Cookies to store the number
A. of times a user has visited servlet.
B. Develop servlet application to demonstrate the use of
session management.
C. 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 index.html page and an error message will be
displayed.
D. Create a servlet demonstrating the use of session

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 2


creation and destruction. Also count the number of
times the user visited the page.
3. Implement the Servlet IO.
Develop a web application using Java Servlets, to
A. provide a set of question [3 Questions minimum] to the
user. Each question should have four options, from
which the user should be able to selection only one.
When the user clicks on button “Submit”, call a servlet
to compute and display the score of the user [number of
correct and incorrect answers].
4. Implement the following JSP applications.
A. Develop a simple JSP application to display values
obtained from the use of implicit objects of various
types.
B. Develop a JSP application to allow the user to modify
the registration details after the valid authentication.
C. Write a simple JSP application to demonstrate the use of
implicit objects [3 methods of 4 object ].
D. Write a JSP code to accept an employee id from user
and delete the records of that employee. If the
employee id does not exist, it should display the
message “No such employee ID”.
E. Develop a JSP application to authenticate user login
with database.
F. Write a JSP code to accept an Department id from user
& display all the employee working in that department.
Display the output in table format.
G. Create a registration and login JSP application to register
and authenticate the user based on username and
password using JDBC.
H. Write a JSP to accept employee number, name and
salary. Insert these records in employee table.
I. Write a JSP application to authenticate user.
J.
Develop a simple JSP application to pass values from
one page to another with validations.
5. Implement the following JSP JSTL and EL Applications
A. Create a JSP application to demonstrate the use of JSTL.
B. Create an html page with fields, employee number,

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 3


name, age, designation, salary. Now on submit this data
to a JSP page which will update the employee table of
database with matching employee number.
C. Create a JSP application to demonstrate the use of
Expression Language [EL].
D. Develop a JSP application to demonstrate use of JSP
Action Tags.
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 for Question & Answer Quiz.
D. Develop a web application to allow the user to enter
inventory details [Product name, Product Price and
Quantity] in JDBC.
E. Write EJB that calculates simple interest and compound
interest for principal, number of years and rate of
interest.
F. Develop a three page web application using any two or
three java EE technology(Student Registration form
using EJB & JDBC)
G. Develop simple application for Question & Answer Quiz
using JDBC.
7. Implement the following EJB applications with
different types of Beans.
A. Develop simple Marks Entry Application to demonstrate
accessing Database using EJB
B. Develop simple application to demonstrate Session
Bean using EJB.
C. Develop simple EJB application to demonstrate Servlet
Hit count using Singleton Session Beans.

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 4


PRACTICAL No : 1
Aim: Implement the following Simple Servlet applications.
A: Create a servlet for a login page. If the username and password are correct
then it says message “Hello” else a message “login failed”.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 1</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Login</h2>
<form method="Post" action="Servlet1">
Enter Username: <input type="text" name="u1"> <br><br>
Enter Password: <input type="password" name="p1"> <br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet1.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/Servlet1"})
public class Servlet1 extends HttpServlet
{
protected void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 5


{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");

if (uname.equals("Tyit") && pass.equals("admin"))


{
out.println("<h1><center>Hello "+ uname +"</center></h1>");
}
else
{
out.println("<h1><center>Login Fails</center></h1>");
}
}
}
}

Output:

If user is Valid:

If user is Invalid:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 6


B: 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.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 2</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Registration</h1>
<form method="Post" action="Servlet2">
Enter UserName: <input type="text" name="u1"><br><br>
Enter Password: <input type="password" name="p1"><br><br>
Enter Email: <input type="text" name="e1"><br><br>
Enter Country: <input type="text" name="c1"><br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet2.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

@WebServlet(urlPatterns = {"/Servlet2"})
public class Servlet2 extends HttpServlet
{
protected void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 7


try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");
String email=request.getParameter("e1");
String country=request.getParameter("c1");

try
{
Connection con = DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("INSERT INTO STUDENT VALUES
(?,?,?,?)");

ps.setString(1,uname);
ps.setString(2,pass);
ps.setString(3,email);
ps.setString(4,country);

ps.executeUpdate();

out.println("<center><h1>Registration Successful</h1></center>");
}
catch(SQLException e)
{
System.out.println(e);
}
}
}
}

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 8


Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 9


C: Write a servlet code to store the biodata details of a user into the
database. (Name, age, address, hobbies, gender, Qualification).
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 3</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>BioData</h1>
<form method="Post" action="Servlet3">
Enter Name: <input type="text" name="n1"><br><br>
Enter Age: <input type="text" name="age"><br><br>
Enter Address: <input type="text" name="a1"> <br><br>
Enter Hobbies: <input type="text" name="h1"><br><br>
Select Gender:
<input type="radio" name="r1" value="Male">Male
<input type="radio" name="r1" value="Female">Female
<br><br>
Enter Qualification: <input type="text" name="q1"><br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet3.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

@WebServlet(urlPatterns = {"/Servlet3"})
public class Servlet3 extends HttpServlet
{

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 10


protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String name=request.getParameter("n1");
int age=Integer.parseInt(request.getParameter("age"));
String address=request.getParameter("a1");
String hobbies=request.getParameter("h1");
String gender=request.getParameter("r1");
String qual=request.getParameter("q1");

try
{
Connection con
=DriverManager.getConnection("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("INSERT INTO Emp VALUES
(?,?,?,?,?,?)");

ps.setString(1,name);
ps.setInt(2,age);
ps.setString(3,address);
ps.setString(4,hobbies);
ps.setString(5,gender);
ps.setString(6,qual);

ps.executeUpdate();

out.println("<center><h2>Data Is Successfully Stored</h2></center>");


}
catch(SQLException e)
{
System.out.println(e);
}
}
}
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 11


Output:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 12


D: Write a Java Program using Servlet to authenticate user’s username and
password.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 4</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Login</h2>
<form method="Post" action="Servlet4">
Enter Username: <input type="text" name="u1"><br><br>
Enter Password: <input type="password" name="p1"><br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet4.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/Servlet4"})
public class Servlet4 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 13


if (uname.equals("Tyit") && pass.equals("admin"))
{
out.println("<h1><center>Hello "+ uname +"</center></h1>");
}
else
{
out.println("<h1><center>Login Fails</center></h1>");
}
}
}
}

Output:

If user is Valid:

If user is Invalid:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 14


E: Develop servlet application for accepting user registration details and
displaying it on another page (At least 6 Values).
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 5</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Registration</h1>
<form method="Post" action="Servlet5">
Enter Name: <input type="text" name="n1"><br><br>
Enter Age: <input type="text" name="age"><br><br>
Enter Address: <input type="text" name="a1">br><br>
Enter Hobbies: <input type="text" name="h1"><br><br>
Select Gender:
<input type="radio" name="r1" value="Male">Male
<input type="radio" name="r1" value="Female">Female
<br><br>
Enter Qualification: <input type="text" name="q1"><br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet5.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/Servlet5"})
public class Servlet5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 15


{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String name=request.getParameter("n1");
int age=Integer.parseInt(request.getParameter("age"));
String address=request.getParameter("a1");
String hobbies=request.getParameter("h1");
String gender=request.getParameter("r1");
String qual=request.getParameter("q1");

out.println("<center><h1>Details</h1>");
out.println("<h2>Name is : " + name);
out.println("<br>Age is : " + age);
out.println("<br>Address is : " + address);
out.println("<br>Hobbies is : " + hobbies);
out.println("<br>Gender is : " + gender);
out.println("<br>Qualification is : " + qual);
out.println("</h2></center>");
} }}

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 16


F: Develop servlet application of basic calculator.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 7</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Basic Calculater</h1>
<form method="Post" action="Servlet7">
Enter First Number <input type="text" name="n1"><br><br>
Enter Second Number <input type="text" name="n2"><br><br>
Select Operator
<select name="op">
<option value="add">Addition</option>
<option value="sub">Subtraction</option>
<option value="multi">Multiplication</option>
<option value="div">Division</option>
</select>
<br><br>
<input type="submit" value="Answer">
</form>
</center>
</body>
</html>

Servlet7.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;

@WebServlet(urlPatterns = {"/Servlet7"})
public class Servlet7 extends HttpServlet
{

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 17


protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
int num1=Integer.parseInt(request.getParameter("n1"));
int num2=Integer.parseInt(request.getParameter("n2"));

String operation=request.getParameter("op");

RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);

switch(operation)
{
case "add":
{
out.println("<center><h2>Addition is : "+ (num1+num2) +"</h2></center>");
break;
}
case "sub":
{
out.println("<center><h2>Subtraction is : "+ (num1-num2) +"</h2></center>");
break;
}
case "multi":
{
out.println("<center><h2>Multiplication is : "+ (num1*num2)
+"</h2></center>");
break;
}
case "div":
{
out.println("<center><h2>Division is : "+ (num1/num2) +"</h2></center>");
break;
}
default:
{
out.println("<center><h2>Invalid Inputs</h2></center>");
break;
}
}
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 18


}
}

Output:

After performing Multiplication:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 19


G: Developed a servlet application to demonstrate simple validation. And If
user left any filed empty [Username or Password] then user will stay on the
index.html page and an error message will be displayed.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 12</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Login</h2>
<form method="Post" action="Servlet12">
UserName <input type="text" name="u1"><br><br>
Password <input type="password" name="p1"><br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Success.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Welcome</h2>
</center>
</body>
</html>

Servlet12.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 20


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;

@WebServlet(urlPatterns = {"/S12"})
public class S12 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");

if (uname.equals("") && pass.equals(""))


{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
out.println("<center><h2>Please Enter Username and Password</h2></center>");
}

else if (uname.equals(""))
{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
out.println("<center><h2>Please Enter Username</h2></center>");
}

else if (pass.equals(""))
{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
out.println("<center><h2>Please Enter Password</h2></center>");
}

else
{
RequestDispatcher rd=request.getRequestDispatcher("success.html");
rd.forward(request,response);
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 21


}
}
}

Output:

If Username or Password is Empty:

H: Create a servlet which authenticate with Database .

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 22


Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 13</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Login</h2>
<form method="Post" action="Servlet13">
UserName <input type="text" name="u1">
<br><br>
Password <input type="password" name="p1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet13.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

@WebServlet(urlPatterns = {"/Servlet13"})
public class Servlet13 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 23


try
{
Connection con
=DriverManager.getConnection("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("SELECT * FROM STUDENT WHERE
USERNAME= ? and PASSWORD= ?");

ps.setString(1,uname);
ps.setString(2,pass);

ResultSet rs= ps.executeQuery();

if (rs.next())
{
out.println("<center><h2>Valid User</h2></center>");
}

else
{
out.println("<center><h2>Invalid User</h2></center>");
}
rs.close();
ps.close()
}
catch(SQLException e)
{
System.out.println(e);
}
}
}
}

Output:

If user is Valid:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 24


If user is Invalid:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 25


PRACTICAL No : 2
Aim: Implement the following Servlet applications with Cookies and Sessions.

A: Create a servlet that uses Cookies to store the number of times a user has
visited servlet.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 6</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<form method="Post" action="Servlet6">
UserName <input type="text" name="u1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet6.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;

@WebServlet(urlPatterns = {"/Servlet6"})
public class Servlet6 extends HttpServlet
{
static int i=1;

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 26


{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
Cookie c=new Cookie("Visit",String.valueOf(i));
response.addCookie(c);
int j=Integer.parseInt(c.getValue());

if (j==1)
{
out.println("<center><h1>Welcome "+ uname + "</h1></center>");
}

else
{
out.println("<center><h1>You have Visited "+j+" Times </h1></center>" );
}
i++;
}
}
}

Output:

When you visited first time:

When you visited again and again:

B: Develop servlet application to demonstrate the use of session


management.

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 27


Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 8</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="Post" action="Servlet8">
UserName <input type="text" name="u1"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Servlet8.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = {"/Servlet8"})
public class Servlet8 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String n=request.getParameter("u1");

out.println("Hello "+ n);

HttpSession session=request.getSession();
session.setAttribute("user",n);

out.println("<form method='Post' action='S8'><br><br>");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 28


out.println("<input type='submit' value='Go to Next'>");
out.println("</form>");

}
}
}

S8.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = {"/S8"})
public class S8 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
HttpSession session=request.getSession(false);

String s=(String)session.getAttribute("user");
out.println("Welcome "+ s);
}
}
}

Output:

Servlet1:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 29


Servlet2:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 30


C: 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
index.html page and an error message will be displayed.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 9</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Login</h2>
<form method="Post" action="Servlet9">
UserName <input type="text" name="u1">
<br><br>
Password <input type="password" name="p1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Success.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Welcome</h2>
</center>
</body>
</html>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 31


Servlet9.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;

@WebServlet(urlPatterns = {"/Servlet9"})
public class Servlet9 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");

if (pass.equals("servlet"))
{
RequestDispatcher rd=request.getRequestDispatcher("success.html");
rd.forward(request,response);
}
else
{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
out.println("<center><h2>Invalid user</h2></center>");
}
}
}
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 32


Output:

If user is Valid:

If user is Invalid:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 33


D: Create a servlet demonstrating the use of session creation and
destruction. Also count the number of times the user visited the page.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 10</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Login</h2>
<form method="Post" action="Servlet10">
UserName <input type="text" name="u1"><br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet10.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = {"/Servlet10"})
public class Servlet10 extends HttpServlet
{
static int i=1;

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 34


String n=request.getParameter("u1");
HttpSession session=request.getSession(true);

out.println("<center><h2>Hello "+ n);


out.println("<br><br>You have Visited "+i+" Times</h2></center>" );

session.invalidate();
i++;
}
}
}

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 35


PRACTICAL No : 3
Aim: Implement the Servlet IO and File applications.
A: Develop a web application using Java Servlets, to provide a set of question
[3 Questions minimum] to the user. Each question should have four options,
from which the user should be able to selection only one. When the user
clicks on button “Submit”, call a servlet to compute and display the score of
the user [number of correct and incorrect answers].
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 11</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="Post" action="Servlet11">
<h4>Welcome to MCQ Quiz Game </h4>

1. Who invented Java Programming?


<br>
<input type="radio" name="r1" value="a">A) Steve Jobs
<br>
<input type="radio" name="r1" value="b">B) James Gosling
<br>
<input type="radio" name="r1" value="c">C) Dennis Ritchie
<br>
<input type="radio" name="r1" value="d">D) Guido van Rossum
<br><br>

2. JDK stand for________?


<br>
<input type="radio" name="r2" value="a">A) Java development kit;
<br>
<input type="radio" name="r2" value="b">B) Java deployment kit;
<br>
<input type="radio" name="r2" value="c">C) JavaScript deployment kit;
<br>
<input type="radio" name="r2" value="d">D) None of these;
<br><br>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 36


3. Which component is used to compile, debug and execute the java programs?
<br>
<input type="radio" name="r3" value="a">A) JRE
<br>
<input type="radio" name="r3" value="b">B) JIT
<br>
<input type="radio" name="r3" value="c">C) JDK
<br>
<input type="radio" name="r3" value="d">D) JVM
<br><br>

<input type="submit" value="Submit">


</form>
</body>
</html>

Servlet11.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/Servlet11"})
public class Servlet11 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String q1=request.getParameter("r1");
String q2=request.getParameter("r2");
String q3=request.getParameter("r3");

int score=0;

if (q1 != null && q1.equals("b"))


{
score++;

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 37


}
if (q2 != null && q2.equals("a"))
{
score++;
}

if (q3 != null && q3.equals("c"))


{
score++;
}

out.println("<center><h2>Correct Answer is : " + score +"</br>");


out.println("Incorrect Answer is : " + (3-score)+ "</center></h2>");

}
}
}

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 38


PRACTICAL No: 4
Aim: Implement the following JSP applications.
A: Develop a simple JSP application to display values obtained from the use
of implicit objects of various types.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Implicit objects</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<form method="post" action="implicit.jsp">
Enter Name:<input type="text" name="n1"><br><br>
Enter Password:<input type="password" name="p1"><br><br>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>

implicit.jsp
<%@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>
<center>
<h1>Use of Intrinsic Objects in JSP </h1>
<h2>Request Object</h2>
Query String<%=request.getQueryString() %><br>
Context Path<%=request.getContextPath() %><br>
Remote Host<%=request.getRemoteHost() %><br><br>

<h2>Response Object</h2>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 39


Character Encoding Type<%=response.getCharacterEncoding() %><br>
Content Type<%=response.getContentType()%><br>
Local <%=response.getLocale()%><br> <br>

<h2>Session Object</h2>
ID<%=session.getId()%><br>
Creation Time<%=new java.util.Date(session.getCreationTime()) %><br>
Last Access Time<%=new java.util.Date(session.getLastAccessedTime())%>
</center>
</body>
</html>

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 40


B: Develop a JSP application to allow the user to modify the registration
details after the valid authentication.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Login</h1>
<form method="post" action="check.jsp">
Enter Username: <input type="text" name="n1"><br><br>
Enter Password: <input type="password" name="p1"><br><br>
<input type="submit" value="Login">
</form>
</center>
</body>
</html>

check.jsp
<%@page import="java.sql.*" %>
<%@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>
<%
String uname=request.getParameter("n1");
String pass=request.getParameter("p1");

try
{
Connection con=DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps= con.prepareStatement("SELECT * FROM STUDENT_Table
WHERE NAME= ? and PASSWORD= ?");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 41


ps.setString(1,uname);
ps.setString(2,pass);

ResultSet rs =ps.executeQuery();

if(rs.next())
{
out.println("<center><h1>Login Successful</h1>");
%>
<a href="update.html">Update Values</a>
<%
out.println("</center>");
}
else
{
out.println("<center><h1>Login Fails</h1>");
%>
<a href="index.html">Try again</a>
<%
out.println("</center>");
}
rs.close();
ps.close();
}
catch(SQLException e)
{
System.out.println(e);
}
%>
</body>
</html>

update.html
<!DOCTYPE html>
<html>
<head>
<title>Update Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Update Data</h1>
<form method="post" action="update.jsp">

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 42


Enter Id: <input type="text" name="id"><br><br>
Enter Username: <input type="text" name="n1"><br><br>
Enter Password: <input type="password" name="p1"><br><br>
Enter Course: <input type="text" name="c1"><br><br>
Enter Hobbies: <input type="text" name="h1"><br><br>
<input type="submit" value="Update">
</form>
</center>
</body>
</html>

update.jsp
<%@page import="java.sql.*" %>
<%@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>
<%
int id=Integer.parseInt(request.getParameter("id"));
String uname=request.getParameter("n1");
String pass=request.getParameter("p1");
String course=request.getParameter("c1");
String hobbies=request.getParameter("h1");

try
{
Connection con=DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps= con.prepareStatement("UPDATE STUDENT_Table SET
NAME=?, PASSWORD=?,COURSE=?,HOBBIES=? WHERE ID=?");

ps.setString(1,uname);
ps.setString(2,pass);
ps.setString(3,course);
ps.setString(4,hobbies);
ps.setInt(5,id);

ps.executeUpdate();

ps.close();

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 43


con.close();

out.println("<center><h1>Value Updated Successfuly.</h1></center>");


}
catch(SQLException e)
{
System.out.println(e);
}
%>
</body>
</html>

Output:

If user is Invalid:

If user is Valid:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 44


Updation Successful:

Database before updation:

Database after updation:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 45


C: Write a simple JSP application to demonstrate the use of implicit objects [3
methods of 4 object ].
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Implicit objects</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Login </h1>
<form method="post" action="implicit.jsp">
Enter Username: <input type="text" name="n1"><br><br>
Enter Password: <input type="password" name="p1"><br><br>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>

implicit.jsp
<%@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>
<center>
<%
String uname = request.getParameter("n1");
String pass = request.getParameter("p1");

session.setAttribute("Username", uname);
session.setAttribute("Password", pass);

response.setContentType("text/html");
response.setStatus(200);
%>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 46


<h1>Use of Implicit Objects</h1>
<%
out.println("<h3>Hello, " + uname + "!<br><br>");
out.println("Your Password is : " + pass + "</h3>");

%>

<h2>Session Details:</h2>
<h3>
Session ID: <%= session.getId() %><br><br>
Username from session: <%= session.getAttribute("Username") %><br><br>
Password from session: <%= session.getAttribute("Password") %>
</h3>

</center>
</body>
</html>

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 47


D: Write a JSP code to accept an employee id from user and delete the
records of that employee. If the employee id does not exist, it should display
the message “No such employee ID”.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Delete Records </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Delete Records</h1>
<form method="post" action="delete.jsp">
Enter ID to Delete Records: <input type="text" name="id"><br><br>
<input type="submit" value="Delete Records">
</form>
</center>
</body>
</html>

delete.jsp
<%@page import="java.sql.*" %>
<%@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>
<%
int id=Integer.parseInt(request.getParameter("id"));

try
{
Connection
con=DriverManager.getConnection("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps= con.prepareStatement("DELETE FROM STUDENT_Table
WHERE ID= ?");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 48


ps.setInt(1,id);
int rowdelete=ps.executeUpdate();

if(rowdelete>0)
{
out.println("<center><h1>Employee record Deleted Successfully</h1></center>");
}
else
{
out.println("<center><h1>No such Employee ID</h1>");
%>
<a href="index.html">Try again</a>
<%
out.println("</center>");
}
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
%>
</body>
</html>

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 49


Database before deletion:

Database after deletion:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 50


E: Develop a JSP application to authenticate user login with database.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Login Page</h1>
<form method="post" action="check.jsp">
Enter Username: <input type="text" name="n1"><br><br>
Enter Password: <input type="password" name="p1"><br><br>
<input type="submit" value="Login">
</form>
</center>
</body>
</html>

check.jsp
<%@page import="java.sql.*" %>
<%@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>
<%
String uname=request.getParameter("n1");
String pass=request.getParameter("p1");
try {
Connection con=DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps= con.prepareStatement("SELECT * FROM STUDENT_Table
WHERE NAME= ? and PASSWORD= ?");
ps.setString(1,uname);
ps.setString(2,pass);
ResultSet rs =ps.executeQuery();

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 51


if(rs.next()){
out.println("<center><h1>Login Successful</h1>");
out.println("<br><h2>Welcome "+ uname +"</h2></center>");
}
else{
out.println("<center><h1>Login Fails</h1>");
%> <a href="index.html">Try again</a>
<%
out.println("</center>");
}
rs.close();
ps.close();
con.close();
}
catch(SQLException e) {
System.out.println(e);
}
%>
</body>
</html>

Output:

If user is Valid:

If user is Invalid:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 52


F: Write a JSP code to accept an Department id from user & display all the
employee working in that department. Display the output in table format.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>View Records</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>View Records</h1>
<form method="post" action="view.jsp">
Enter Department ID: <input type="text" name="id"><br><br>
<input type="submit" value="View Records">
</form>
</center>
</body>
</html>

View.jsp
<%@page import="java.sql.*"%>
<%@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>
<style>
table,th,td
{
border: 2px solid black;
}
</style>
</head>
<body>
<%
int d_id=Integer.parseInt(request.getParameter("id"));

try
{

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 53


Connection con =DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("SELECT * FROM DEPARTMENT
WHERE D_ID=?");

ps.setInt(1,d_id);
ResultSet rs= ps.executeQuery();
%>
<center>

<h2>Employee Details</h2>
<table>
<tr>
<th>D_Id</th>
<th>D_Name</th>
<th>Emp_Name</th>
<th>Emp_Age</th>
<th>Emp_Salary</th>
</tr>
<%
while(rs.next())
{
out.println("<tr>");
out.println("<td>"+rs.getInt(1)+"</td>");
out.println("<td>"+rs.getString(2)+"</td>");
out.println("<td>"+rs.getString(3)+"</td>");
out.println("<td>"+rs.getInt(4)+"</td>");
out.println("<td>"+rs.getInt(5)+"</td>");
out.println("</tr>");
}
%>
</table>
</center>
<%

rs.close();
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
%>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 54


</body>
</html>

Output:

Employee Records:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 55


G: Create a registration and login JSP application to register and authenticate
the user based on username and password using JDBC.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Registration Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Registration </h1>
<form method="post" action="registration.jsp">
Enter Id: <input type="text" name="id"><br><br>
Enter Username: <input type="text" name="n1"><br><br>
Enter Password: <input type="password" name="p1"><br><br>
Enter Course: <input type="text" name="c1"><br><br>
Enter Hobbies: <input type="text" name="h1"><br><br>
<input type="submit" value="Register"> Already Registered ? <a href="login.html">
Login</a>
</form>
</center>
</body>
</html>

login.html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Login</h1>
<form method="post" action="check.jsp">
Enter Username: <input type="text" name="n1"><br><br>
Enter Password: <input type="password" name="p1"><br><br>
<input type="submit" value="Login"> <a href="index.html"> Register</a>
</form>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 56


</center>
</body>
</html>

registration.jsp
<%@page import="java.sql.*"%>
<%@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>
<%
int id=Integer.parseInt(request.getParameter("id"));
String uname=request.getParameter("n1");
String pass=request.getParameter("p1");
String course=request.getParameter("c1");
String hobbies=request.getParameter("h1");

try
{
Connection con
=DriverManager.getConnection("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("INSERT INTO STUDENT_Table
VALUES (?,?,?,?,?)");

ps.setInt(1,id);
ps.setString(2,uname);
ps.setString(3,pass);
ps.setString(4,course);
ps.setString(5,hobbies);

ps.executeUpdate();

out.println("<center><h1>Registration Successful</h1>");
%>
<a href="login.html">Login</a>
<%
out.println("</center>");
ps.close();
con.close();
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 57


catch(SQLException e)
{
System.out.println(e);
}
%>
</body>
</html>

Check.jsp
<%@page import="java.sql.*"%>
<%@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>
<%
String uname=request.getParameter("n1");
String pass=request.getParameter("p1");

try
{
Connection con =DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("SELECT * FROM STUDENT_Table
WHERE NAME= ? and PASSWORD= ?");

ps.setString(1,uname);
ps.setString(2,pass);

ResultSet rs= ps.executeQuery();

if (rs.next())
{
out.println("<center><h1>Login Successful</h1>");
out.println("<br><h2>Welcome "+ uname+"</h2></center>");
}
else
{
out.println("<center><h1>Login Fails</h1>");
%>
<a href="login.html">Try again</a>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 58


<%
out.println("</center>");
}
rs.close();
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
%>
</body>
</html>

Output:

Login:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 59


If user is Valid:

If user is Invalid:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 60


H: Write a JSP to accept employee number, name and salary. Insert these
records in employee table.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Registration Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Registration </h1>
<form method="post" action="registration.jsp">
Enter Number: <input type="text" name="num"><br><br>
Enter Name: <input type="text" name="n1"><br><br>
Enter Salary: <input type="text" name="s1"><br><br>
<input type="submit" value="Register">
</form>
</center>
</body>
</html>

registration.jsp
<%@page import="java.sql.*"%>
<%@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>
<%
int num=Integer.parseInt(request.getParameter("num"));
String name=request.getParameter("n1");
int sal=Integer.parseInt(request.getParameter("s1"));
try
{
Connection con =DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 61


PreparedStatement ps = con.prepareStatement("INSERT INTO EMPLOYEE VALUES
(?,?,?)");
ps.setInt(1,num);
ps.setString(2,name);
ps.setInt(3,sal);
ps.executeUpdate();
out.println("<center><h1>Registration Successful</h1></center>");
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
%>
</body>
</html>

Output:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 62


I: Write a JSP application to authenticate user.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Login</h1>
<form method="post" action="check.jsp">
Enter Name: <input type="text" name="n1"><br><br>
Enter Password: <input type="password" name="p1"><br><br>
<input type="submit" value="Login">
</form>
</center>
</body>
</html>

check.jsp
<%@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>
<%
String uname=request.getParameter("n1");
String pass=request.getParameter("p1");

if (uname.equals("Ram") && pass.equals("ram123"))


{
out.println("<center><h2>Welcome Ram</h2></center>");
}
else
{
out.println("<center><h2>Invalid User</h2>");
%>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 63


<a href="index.html">Try again</a>
<%
out.println("</center>");
}
%>
</body>
</html>

Output:

If user is Vaild:

If user is Invaild:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 64


J: Develop a simple JSP application to pass values from one page to another
with validations.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Registration Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Registration</h1>
<form method="post" action="check.jsp">
Enter your Name : <input type="text" name="n1"><br><br>
Enter your Age : <input type='text' name="a1"><br><br>
Hobbies : <input type='text' name="h1"><br><br>
Gender :
<input type="radio" name="r1" value="Male">Male
<input type="radio" name="r1" value="Female">Female<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

check.jsp
<%@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> <%
String name=request.getParameter("n1");
int age = Integer.parseInt(request.getParameter("a1"));
String hob = request.getParameter("h1");
String gender = request.getParameter("r1");
boolean isValid = true;
if(name == null || age <=0 || gender ==null || hob == null) {
isValid = false;

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 65


}
if(isValid) {
out.println("<center><h1>Details</h1>");
out.println("<h2>Name : "+name);
out.println("<br>Age : "+age);
out.println("<br>Hobbies : "+hob);
out.println("<br>Gender : "+gender+"</h2></center>");
}
else {
out.println("<center><h1>Data Invalid Failed</h1>");
out.println("<h2>Please check the entered data</h2>"); %>
<a href="index.html">Try again</a> <%
out.println("</center>");
} %>
</body>
</html>

Output:

If any filed is Empty:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 66


PRACTICAL No : 5
Aim: Implement the following JSP JSTL and EL Applications
A: Create a JSP application to demonstrate the use of JSTL.
Index.html
<!DOCTYPE html>
<html>
<head>
<title> JSTL </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Login </h1>
<form method="post" action="JSTL.jsp">
Enter Username: <input type="text" name="n1"><br><br>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>

JSTL.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@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>
<center>
<h2>CORE Tag</h2>
<%
String uname=request.getParameter("n1");
%>
<c:set var="name" value="<%=uname%>"/>
<c:choose>
<c:when test="${name eq 'Harsh'}">

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 67


<c:out value="Valid user"/><br><br>
<c:out value="Welcome, ${name}!"/>
</c:when>
<c:otherwise>
<c:out value="Invalid user"/><br><br>
<c:out value="Welcome, Guest!"/>
</c:otherwise>
</c:choose> <br><br>

<h2>FUNCTION Tag</h2>
<c:set var="string1" value="HARSH"/>

Lower String : ${fn:toLowerCase(string1)}<br>


<c:set var="string2" value="Ram"/>

Upper String : ${fn:toUpperCase(string2)}<br>


Length of String1 : ${fn:length(string1)}<br>
Length of String1 : ${fn:length(string2)}<br>

<c:set var="string3" value="Monkey D. Luffy"/>


Replace String : ${fn:replace(string3,"Luffy","Dragon")}
</center>
</body>
</html>

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 68


B: Create an html page with fields, employee number, name, age,
designation, salary. Now on submit this data to a JSP page which will update
the employee table of database with matching employee number.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Update Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Update Records</h1>
<form method="post" action="update.jsp">
Employee Number: <input type="text" name="num"><br><br>
Employee Name: <input type="text" name="n1"><br><br>
Employee Age: <input type="text" name="a1"><br><br>
Employee Designation: <input type="text" name="d1"><br><br>
Employee Salary: <input type="text" name="s1"><br><br>
<input type="submit" value="Update">
</form>
</center>
</body>
</html>

Update.jsp
<%@page import="java.sql.*" %>
<%@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>
<%
int num=Integer.parseInt(request.getParameter("num"));
String name=request.getParameter("n1");
int age=Integer.parseInt(request.getParameter("a1"));
String des=request.getParameter("d1");
int sal=Integer.parseInt(request.getParameter("s1"));

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 69


try
{
Connection con=DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps= con.prepareStatement("UPDATE EMP_DATA SET
NAME=?,AGE=?,"+ "DESIGNATION=?,SALARY=? WHERE NUMBER=?");

ps.setString(1,name);
ps.setInt(2,age);
ps.setString(3,des);
ps.setInt(4,sal);
ps.setInt(5,num);

int rowupdate=ps.executeUpdate();

if(rowupdate>0)
{
out.println("<center><h1>Employee record Update Successfully</h1></center>");
}
else
{
out.println("<center><h1>No such Employee Exist</h1>");
%>
<a href="index.html">Try again</a>
<%
out.println("</center>");
}
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
%>
</body>
</html>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 70


Output:

If Employee Exist:

If Employee does not Exist:

Database before Updation:

Database after Updation:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 71


C: Create a JSP application to demonstrate the use of Expression Language
[EL].
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Login</h1>
<form method="post" action="check.jsp">
Enter Name: <input type="text" name="n1"><br><br>
Enter Password: <input type="password" name="p1"><br><br>
<input type="submit" value="Login">
</form>
</center>
</body>
</html>

check.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@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>
<%
String uname=request.getParameter("n1");
String pass=request.getParameter("p1");

if (uname.equals("Harsh") && pass.equals("harsh17"))


{
session.setAttribute("authenticated", true);
session.setAttribute("username", uname);
}
else

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 72


{
session.setAttribute("authenticated", false);
}
%>
<center>

<c:if test="${sessionScope.authenticated}">
<h2>Welcome, ${sessionScope.username}!</h2>
<p>You have successfully logged in.</p>
</c:if>
<c:if test="${!sessionScope.authenticated}">
<h2>Login Failed</h2>
<p>Invalid username or password. Please try again.</p>
<a href="index.html">Go back to Login</a>
</c:if>

</center>
</body>
</html>

Output:

If user is Vaild:

If user is Invaild:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 73


D: Develop a JSP application to demonstrate use of JSP Action Tags.
Index.html [<jsp:include> tag]
<!DOCTYPE html>
<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>
<center>
<h2>JSP Action Tags Demo</h2>
<form method="post" action="display.jsp">
Enter your name: <input type="text" name="n1"><br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

display.jsp
<%@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>
<%
String name=request.getParameter("n1");
out.println("<center><h1>Hello "+name+ "</center></h1>");
%>
<jsp:include page="include.jsp">
<jsp:param name="user" value= "<%= name %>" />
</jsp:include>
</body>
</html>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 74


Include.jsp
<%@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>
<%
out.println("<br>");
String uname = request.getParameter("user");
out.println("<center><h1>Welcome " + uname+ "</center></h1>");
%>
</body>
</html>
Output:

Index.html [<jsp:forward> tag]


<!DOCTYPE html>
<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>
<center>
<h2>JSP Action Tags Demo</h2>
<form method="post" action="display.jsp">
Enter your name: <input type="text" name="n1"><br><br>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 75


<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

display.jsp
<%@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>
<%
String name=request.getParameter("n1");
out.println("<center><h1>Hello "+name+ "</center></h1>");
%>
<jsp:forward page="forward.jsp">
<jsp:param name="user" value= "<%= name %>" />
</jsp:forward>
</body>
</html>

forward.jsp
<%@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>
<center>
<h1>
<%
String n = request.getParameter("user");
out.println("Hello "+n);
%>
</h1>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 76


</center>
</body>
</html>

Output:

Index.html [<jsp:useBean> <jsp:setProperty> <jsp:getProperty> Tags]


<!DOCTYPE html>
<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>
<center>
<h2>JSP Action Tags Demo</h2>
<form method="post" action="display.jsp">
First Name : <input type="text" name="first"><br><br>
Last Name : <input type="text" name="last"><br><br>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 77


display.jsp
<%@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>
<jsp:useBean id="pack1" class="mypackage.action"></jsp:useBean>
<jsp:setProperty property="*" name="pack1"></jsp:setProperty>
<center><h1>Information <br></h1>
<h2>First Name :<jsp:getProperty property="first"
name="pack1"></jsp:getProperty><br>
Last Name :<jsp:getProperty property="last" name="pack1"></jsp:getProperty></h2>
</center>
</body>
</html>

action.java [session bean]


package mypackage;
import javax.ejb.Stateless;
@Stateless
public class action
{
public String first;
public String last;
public void setfirst(String first)
{
this.first=first;
}
public void setlast(String last)
{
this.last=last;
}
public String getfirst()
{
return first;
}
public String getlast()

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 78


{
return last;
}
}

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 79


PRACTICAL No : 6
Aim: Implement the following EJB Applications.
A: Create a Currency Converter application using EJB.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Currency Converter</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<form method="post" action="convert.jsp">
Enter amount: <input type="text" name="e1"><br><br>
Dolor To Rupees <input type="radio" name="r1" value="d2r"><br><br>
Rupees To Dollar<input type="radio" name="r1" value="r2d"><br><br>
<input type="submit" value="Convert">
</form>
</center>
</body>
</html>

Convert.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"
import="mypackage.MoneyConverter;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
float currency = Float.parseFloat(request.getParameter("e1"));
String str = request.getParameter("r1");
MoneyConverter ob = new MoneyConverter();

if(str.equals("d2r")) {
float usd = ob.DtoR(currency);
out.println("<center><h3>"+currency + " $ = " +usd +" Rs</h3></center>");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 80


}
else {
float inr = ob.RtoD(currency);
out.println("<center><h3>"+currency +" Rs = " +inr +" $</h3></center>");
}
%>
</body>
</html>

MoneyConverter.java [session bean]


package mypackage;
import javax.ejb.Stateless;
@Stateless
public class MoneyConverter {
public float DtoR(float currency) {
return 86.9f * currency;
}
public float RtoD(float currency) {
return currency / 86.9f;
}
}

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 81


B: Develop a Simple Room Reservation System Application Using EJB.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Room Reservation</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Room Reservation</h2>
<form method="post" action="insert.jsp">
Enter your Name : <input type="text" name="name"><br><br>
Select Room Type : <br>
<input type="radio" name="n1" value="deluxe"> Deluxe<br>
<input type="radio" name="n1" value="super"> Super<br>
<input type="radio" name="n1" value="normal"> Normal<br>
<input type="radio" name="n1" value="single"> Single<br><br>
Enter Room Number : <input type="text" name="num"><br><br>
<input type="submit" value="Room Book">
</form>
</center>
</body>
</html>

JSTL.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"
import="mypackage.RoomReservation"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
int rnum = Integer.parseInt(request.getParameter("num"));
String name = request.getParameter("name");
String rtype= request.getParameter("n1");

RoomReservation ob = new RoomReservation();

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 82


String message = ob.room_booking(rnum,name,rtype);
out.println("<center><h2> " + message +"</center></h2>");
%>
</body>
</html>

RoomReservation.java [session bean]


package mypackage;
import java.sql.*;
import javax.ejb.Stateless;
@Stateless
public class RoomReservation {
public String room_booking(int rnum,String name, String rtype)
{
try
{
Connection con = DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");

PreparedStatement ps = con.prepareStatement("INSERT INTO ROOMS


VALUES(?,?,?)");

ps.setInt(1,rnum);
ps.setString(2,name);
ps.setString(3,rtype);

ps.executeUpdate();
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
return "Room Booked Successfully.";
}
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 83


Output:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 84


C: Develop simple application for Question & Answer Quiz.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Quiz Game</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="Post" action="check.jsp">
<h4>Welcome to MCQ Quiz Game </h4>
1. Who invented Java Programming? <br>
<input type="radio" name="r1" value="a">A) Steve Jobs<br>
<input type="radio" name="r1" value="b">B) James Gosling <br>
<input type="radio" name="r1" value="c">C) Dennis Ritchie<br>
<input type="radio" name="r1" value="d">D) Guido van Rossum<br><br>

2. JDK stand for________?<br>


<input type="radio" name="r2" value="a">A) Java development kit; <br>
<input type="radio" name="r2" value="b">B) Java deployment kit; <br>
<input type="radio" name="r2" value="c">C) JavaScript deployment kit; <br>
<input type="radio" name="r2" value="d">D) None of these; <br><br>

3. Which component is used to compile, debug and execute the java programs? <br>
<input type="radio" name="r3" value="a">A) JRE <br>
<input type="radio" name="r3" value="b">B) JIT<br>
<input type="radio" name="r3" value="c">C) JDK<br>
<input type="radio" name="r3" value="d">D) JVM<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

check.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" import="mypackage.quiz;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 85


<body>
<%
String q1=request.getParameter("r1");
String q2=request.getParameter("r2");
String q3=request.getParameter("r3");
quiz ob= new quiz();
int score=ob.check_answer(q1, q2, q3);
out.println("<center><h2>Correct Answer is : " + score +"</br>");
out.println("Incorrect Answer is : " + (3-score)+ "</center></h2>");
%>
</body>
</html>

quiz.java [session bean]


package mypackage;
import javax.ejb.Stateless;
@Stateless
public class quiz {
public int check_answer(String a1,String a2,String a3)
{
int score=0;
if (a1 != null && a1.equals("b"))
{
score++;
}
if (a2 != null && a2.equals("a"))
{
score++;
}
if (a3 != null && a3.equals("c"))
{
score++;
}
return score;
}
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 86


Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 87


D: Develop a web application to allow the user to enter inventory details
[Product name, Product Price and Quantity] in JDBC.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Product</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Enter Details</h2>
<form method="post" action="insert.jsp">
Enter Product Name : <input type="text" name="n1"><br><br>
Enter Product Price : <input type="text" name="p1"><br><br>
Enter Quantity : <input type="text" name="q1"><br><br>
<input type="submit" value="Store">
</form>
</center>
</body>
</html>

insert.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"
import="mypackage.product_data;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String pn = request.getParameter("n1");
double pr = Double.parseDouble(request.getParameter("p1"));
int pq = Integer.parseInt(request.getParameter("q1"));

product_data ob = new product_data();


String str= ob.insert_data(pn, pr, pq);

out.println("<center><h2>"+ str + "</h2></center>");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 88


%>
</body>
</html>

product_data.java [session bean]


package mypackage;
import javax.ejb.Stateless;
import java.sql.*;

@Stateless
public class product_data {

public String insert_data(String d1,double d2,int d3)


{
try
{
Connection con = DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");

PreparedStatement ps = con.prepareStatement("INSERT INTO PRODUCT_DATA


VALUES(?,?,?)");

ps.setString(1,d1);
ps.setDouble(2,d2);
ps.setInt(3,d3);

ps.executeUpdate();
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
return "Data Inserted Successfully.";
}
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 89


Output:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 90


E: Write EJB that calculates simple interest and compound interest for
principal, number of years and rate of interest.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Calculate SI & CI</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Calculate SI & CI</h2>
<form method="post" action="calculate.jsp">
Enter a Principal Amount: <input type="text" name="p1"><br><br>
Enter a Interest Rate: <input type="text" name="r1"><br><br>
Enter a number of Year: <input type="text" name="y1"><br><br>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>

calculate.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"
import="mypackage.InterestCalculator;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
double p=Double.parseDouble(request.getParameter("p1"));
double r=Double.parseDouble(request.getParameter("r1"));
double y=Double.parseDouble(request.getParameter("y1"));

InterestCalculator ic = new InterestCalculator();


double SI = ic.calculateSI(p, y, r);
double CI = ic.calculateCI(p, y, r);
out.println("<center><h2>Simple Interest = " + SI +"</h3></center>\n");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 91


out.println("<center><h2>Compound Interest = " + CI +"</h3></center>");
%>
</body>
</html>

InterestCalculator.java [session bean]


package mypackage;
import javax.ejb.Stateless;
@Stateless
public class InterestCalculator {
public double calculateSI(double p , double y , double r)
{
double SI = (p*r*y)/100;
return SI;
}
public double calculateCI(double p , double y , double r)
{
int n = 12;
double CI = p*(Math.pow(1+(r/(n*100)),n*y))-p;
return CI;
}
}

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 92


F: Develop a three page web application using any two or three java EE
technology(Student Registration form using EJB & JDBC)
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Student Registration </h1>
<form method="post" action="insert.jsp">
Enter Name: <input type="text" name="n1"><br><br>
Enter Roll Number: <input type="text" name="r1"><br><br>
Enter Age: <input type="text" name="a1"><br><br>
Enter Course: <input type="text" name="c1"><br><br>
Enter Hobbies: <input type="text" name="h1"><br><br>
<input type="submit" value="Register">
</form>
</center>
</body>
</html>

insert.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"import="mypackage.register"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String name=request.getParameter("n1");
int roll=Integer.parseInt(request.getParameter("r1"));
int age=Integer.parseInt(request.getParameter("a1"));
String course=request.getParameter("c1");
String hobbies=request.getParameter("h1");

register ob= new register();

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 93


String str=ob.add_data(name,roll,age,course,hobbies);

out.println("<center><h2>" + str + "</center></h2>");


%>
</body>
</html>

register.java [session bean]


package mypackage;

import javax.ejb.Stateless;
import java.sql.*;

@Stateless
public class register {

public String add_data(String name, int roll, int age, String course,String hobbies)
{
try
{
Connection con = DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");

PreparedStatement ps = con.prepareStatement("INSERT INTO STUD


VALUES(?,?,?,?,?)");

ps.setString(1,name);
ps.setInt(2,roll);
ps.setInt(3,age);
ps.setString(4,course);
ps.setString(5,hobbies);

ps.executeUpdate();

ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}

return "Registraction Successfully.";

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 94


}
}

Output:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 95


G: Develop simple application for Question & Answer Quiz using JDBC.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Quiz Game</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="Post" action="check.jsp">
<h4>Welcome to MCQ Quiz Game </h4>
1. Who invented Java Programming? <br>
<input type="radio" name="r1" value="a">A) Steve Jobs<br>
<input type="radio" name="r1" value="b">B) James Gosling <br>
<input type="radio" name="r1" value="c">C) Dennis Ritchie<br>
<input type="radio" name="r1" value="d">D) Guido van Rossum<br><br>

2. JDK stand for________?<br>


<input type="radio" name="r2" value="a">A) Java development kit; <br>
<input type="radio" name="r2" value="b">B) Java deployment kit; <br>
<input type="radio" name="r2" value="c">C) JavaScript deployment kit; <br>
<input type="radio" name="r2" value="d">D) None of these; <br><br>

3. Which component is used to compile, debug and execute the java programs? <br>
<input type="radio" name="r3" value="a">A) JRE <br>
<input type="radio" name="r3" value="b">B) JIT<br>
<input type="radio" name="r3" value="c">C) JDK<br>
<input type="radio" name="r3" value="d">D) JVM<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

check.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" import="mypackage.quiz;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 96


<body>
<%
String q1=request.getParameter("r1");
String q2=request.getParameter("r2");
String q3=request.getParameter("r3");
quiz ob= new quiz();
int score=ob.check_answer(q1, q2, q3);
out.println("<center><h2>Correct Answer is : " + score +"</br>");
out.println("Incorrect Answer is : " + (3-score)+ "</center></h2>");
%>
</body>
</html>

quiz.java [session bean]


package mypackage;
import javax.ejb.Stateless;
import java.sql.*;
@Stateless
public class quiz {
public int check_answer(String a1,String a2,String a3)
{
int score=0,i=0;
String[] arr={a1,a2,a3};
try
{
Connection con
=DriverManager.getConnection("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("SELECT ANSWER FROM QUIZ");

ResultSet rs=ps.executeQuery();

while(rs.next())
{
if (rs.getString(1).equals(arr[i]) )
{
score+=1;
}
i++;
}
ps.close();
con.close();
}
catch(SQLException e)
{

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 97


System.out.println(e);
}
return score;
}
}

Output:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 98


PRACTICAL No : 7
Aim: Implement the following EJB applications with different types of Beans.
A: Develop simple Marks Entry Application to demonstrate accessing
Database using EJB
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Marks Entry</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Marks Entry</h1>
<form method="post" action="marks_entry.jsp">
Enter SPD Marks: <input type="text" name="m1"><br><br>
Enter IOT Marks: <input type="text" name="m2"><br><br>
Enter AWP Marks: <input type="text" name="m3"><br><br>
Enter AI Marks: <input type="text" name="m4"><br><br>
Enter EJava Marks: <input type="text" name="m5"><br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

marks_entry.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" import="insert_data.entry;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
int SPD=Integer.parseInt(request.getParameter("m1"));
int IOT=Integer.parseInt(request.getParameter("m2"));
int AWP=Integer.parseInt(request.getParameter("m3"));

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 99


int AI=Integer.parseInt(request.getParameter("m4"));
int EJava=Integer.parseInt(request.getParameter("m5"));

entry ob=new entry();

String result=ob.insert_marks(SPD,IOT,AWP,AI,EJava);
out.println("<center><h2>" + result + "</center></h2>");
%>
</body>
</html>

entry.java [session bean]


package insert_data;
import javax.ejb.Stateless;
import java.sql.*;
@Stateless
public class entry {
public String insert_marks(int m1,int m2,int m3,int m4,int m5)
{
try
{
Connection con = DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");

PreparedStatement ps = con.prepareStatement("INSERT INTO STUDENTS_MARKS


VALUES(?,?,?,?,?)");

ps.setInt(1,m1);
ps.setInt(2,m2);
ps.setInt(3,m3);
ps.setInt(4,m4);
ps.setInt(5,m5);

ps.executeUpdate();

ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
return "Marks Inserted Successfully.";

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 100


}
}

Output:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 101


B: Develop simple application to demonstrate Session Bean using EJB.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Calculate</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<form method="post" action="result.jsp">
Enter First Number : <input type="text" name="n1"><br><br>
Enter Second Number : <input type="text" name="n2"><br><br>
<input type="submit" value="Addition">
</form>
</center>
</body>
</html>

result.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"
import="mypacakage.addition;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
int num1 = Integer.parseInt(request.getParameter("n1"));
int num2 = Integer.parseInt(request.getParameter("n2"));
addition a = new addition();
int ans = a.Add(num1, num2);
out.println("<center><h1>Addition is : "+ ans +"</h1></center>");
%>
</body>
</html>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 102


addition.java [session bean]
package mypacakage;

import javax.ejb.Stateless;

@Stateless
public class addition
{
public int Add(int n1 , int n2)
{
return n1 + n2;
}

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 103


C: Develop simple EJB application to demonstrate Servlet Hit count using
Singleton Session Beans.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Hit count</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Click</h1>
<form method="post" action="NewServlet">
<br><br>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>

NewServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ejb.EJB;
import mypackage.hitcount;

@WebServlet(urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {

@EJB hitcount obj;


protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 104


out.print("<center><h2>Number of times this Servlet is accessed : "+obj.getCount() +
"</h2></center>");

}
}
}

hitcount.java [session bean]


package mypackage;
import javax.ejb.Singleton;
@Singleton
public class hitcount
{
private int hitCount;
public synchronized int getCount()
{
return hitCount++;
}
}

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 105

You might also like