Praful EJA
Praful EJA
Practical No.:01
Create a simple calculator application using servlet.
Index.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>
<form action="cal.java">
Enter First Number<input type="text" name="txtn1" ><br><br>
Enter Second Number<input type="text" name="txtn2" ><br><br>
Select an Operation <input type="radio" name="opr" value="+">
ADDITION <input type="radio" name="opr" value="-">
SUBTRACTION <input type="radio" name="opr" value="*">
MULTIPLY <input type="radio" name="opr" value="/">
DIVIDE <br><br><input type="reset">
<input type="submit" value="Calculate" >
</form>
</body>
</html>
cal.java-
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
result=a/b;
}
}
}
Practical No.:02
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”.
Index.html-
<!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>
<form action="login" >
Enter User ID<input type="text" name="txtId"><br><br>
Enter Password<input type="password" name="txtPass"><br><br>
<input type="reset"><input type="submit" value=" Click to Login " >
</form>
</body>
</html>
login.java-
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Practical No.:03
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 in details.
Index.html-
<!DOCTYPE html>
<html>
<head>
<title> supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="login.java">
Enter your name<input type="text" name="txt1"><br>
Enter your surname<input type="text" name="txt2"><br>
<input type="submit"><br>
</form>
</body>
</html>
Login.java-
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
}
}
Practical No.:04
Implement the following Servlet applications with Cookies and
Sessions.
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.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>
<form action="login" method="get">
Enter Username: <input type="text" name="un"><br>
Enter Password: <input type="text" name="pass"><br>
<input type="reset"><!-- comment -->
<input type="submit" value="Login">
</form>
</body>
</html>
Login.java-
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
out.println("login failed");
RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
rd.include(request, response);
}
}catch(Exception e){
out.println(e);
}
}
Welcome.java-
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
String un=request.getParameter("un");
String pass=request.getParameter("pass");
out.println("<h1>Welcome User</h1>"+un);
}catch(Exception e){
out.println(e);
}
}
Practical No.:05
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.
Index.html-
<html>
<body>
<form action="Page1">
Enter your name<input type="text" name="txtname"><br>
<input type="submit" value="Click to enter">
</form>
</body>
</html>
Page1.java-
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Page1</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Page1 at " + request.getContextPath() + "</h1>");
out.println(" <body bgcolor=pink>");
String uname=request.getParameter("txtname");
out.println("<h1>Welcome"+uname+"</h1>");
Cookie ck1=new Cookie("usernmame", uname);
Cookie ck2=new Cookie("visit", "1");
response.addCookie(ck1);
response.addCookie(ck2);
out.println("<h1><a href=Page2>Click to visit Page2</a></h1>");
out.println("</body>");
out.println("</html>");
}finally
{
out.close();
}
}
Page2.java-
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Page2</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Page2 at " + request.getContextPath() + "</h1>");
out.println("<body bgcolor=yellow>");
Cookie[] ck=request.getCookies();
for(int i=0; i<ck.length; i++)
{
if (ck[i].getName().equals("visit"))
{
int count=Integer.parseInt(ck[i].getValue())+1;
out.println("<h1>Visit No: "+count+"</h1>");
ck[i]=new Cookie("visit",count+"");
response.addCookie(ck[i]);
}
else
{
out.println("<br>");
out.println(ck[i].getName()+"="+ck[i].getValue());
}
}
out.println("<h1><a href=Page3>click to visit Page3</a></h1>");
out.println("<h1><a href=Page4>click to visit Page4</a></h1>");
out.println("</body>");
out.println("</html>");
}finally
{
out.close();
}
}
Page3.java-
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Page2</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Page2 at " + request.getContextPath() + "</h1>");
out.println("<body bgcolor=yellow>");
Cookie[] ck=request.getCookies();
for(int i=0; i<ck.length; i++)
{
if (ck[i].getName().equals("visit"))
{
int count=Integer.parseInt(ck[i].getValue())+1;
out.println("<h1>Visit No: "+count+"</h1>");
ck[i]=new Cookie("visit",count+"");
response.addCookie(ck[i]);
}
else
{
out.println("<br>");
out.println(ck[i].getName()+"="+ck[i].getValue());
}
}
out.println("<h1><a href=Page3>click to visit Page3</a></h1>");
out.println("<h1><a href=Page4>click to visit Page4</a></h1>");
out.println("</body>");
out.println("</html>");
}finally
{
out.close();
}
}
Practical No.:06
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.
Index.html-
<html>
<head><title>Session Demo</title></head>
<form action="Page1" method="get" >
Enter User ID <input type="text" name="txtName"><br>
<input type="reset" ><input type="submit" >
</form>
</html>
Page1.java-
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
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 = {"/Page1"})
public class Page1 extends HttpServlet {
out.println("</html>");
}
catch(Exception e)
{
out.close();
}
}
Page2.java-
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
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 = {"/Page2"})
public class Page2 extends HttpServlet {
Page3.java-
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
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 = {"/Page3"})
public class Page3 extends HttpServlet {
out.println("</body>");
out.println("</html>");
}
catch(Exception e)
{
out.close();
}
}
Practical No.:07
Develop a simple JSP application to display values obtained
from the use of intrinsic objects of various types.
newjsp.jsp-
<%--
Document : newjsp
Created on : Sep 13, 2023, 5:49:00 PM
Author : admin
--%>
Locale<%=response.getLocale()%><br>
<h1>Session Object</h1>
ID<%=session.getId()%><br>
Creation Time<%=new java.util.Date(session.getCreationTime())%><br>
Last Access Time<%=new java.util.Date(session.getLastAccessedTime())%>
</body>
</html>
Practical No.:08
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).
Index.hmtl-
<!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>
<form action="Validate.jsp">
Enter Your Name<input type="text" name="name"><br>
Enter Your Age<input type="text" name="age"><br>
Select Hobbies
<input type="checkbox" name="hob" value="Singing">Singing
<input type="checkbox" name="hob" value="Reading">Reading
<input type="checkbox" name="hob" value="Football">Football<br>
Enter Email<input type="email"><br>
Select gender
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="other">Other<br>
<input type="hidden" name="error" value="">
<input type="submit" value="Submit form">
</form>
</body>
</html>
Chekerbean.java-
package mypack;
public class CheckerBean
{
private String name, age, hob, email, gender, error;
public CheckerBean()
{
error="";
}
public void setName(String n)
{
name=n;
}
public void setAge(String a)
{
name=a;
}
public void setHob(String h)
{
name=h;
}
public void setEmail(String e)
{
name=e;
}
Validate.jsp-
<%@page contentType="text/html" pageEncoding="UTF-8" import="mypack.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Validation Page</h1>
<jsp:useBean id="obj" scope="request" class="mypack.CheckerBean">
<jsp:setProperty name="obj" property="*"/>
</jsp:useBean>
<%if(obj.validate())
{%>
<jsp:forward page="successful.jsp"/>
<%}
else {%>
<jsp:include page="index.html"/>
<%}%>
<%=obj.getError()%>
</body>
</html>
Successful.jsp-
<%--
Document : successful
Created on : Sep 13, 2023, 7:08:10 PM
Author : admin
--%>
</head>
<body bgcolor="yellow">
<% String name=request.getParameter("name");
out.print("hello "+ name);
%>
</body>
</html>
Practical No.:09
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>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="newjsp1.jsp">
Enter Username<input type="text" name="t1"><br>
Enter password<input type="password" name="t2"><br>
<input type="submit" value="Click to Login"><br>
<a href="Reg.html">Click here to new registration</a>
</form>
</body>
</html>
Register.html-
<!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>
<form action="newjsp.jsp">
Enter name<input type="text" name="t1"><br>
Enter Email<input type="text" name="t2"><br>
Enter Password<input type="password" name="t3"><br>
Enter City<input type="text" name="t4"><br>
<input type="submit" value="Register">
</form>
</body>
</html>
Newjsp.jsp-
<%--
Document : newjsp
Created on : Sep 24, 2023, 10:03:30 PM
Author : admin
--%>
<%
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","kunal7039sutar
");
PreparedStatement pst=con.prepareStatement("insert into NewReg values (?,?,?,?)");
pst.setString(1, request.getParameter("t1"));
pst.setString(2, request.getParameter("t2"));
pst.setString(3, request.getParameter("t3"));
pst.setString(4, request.getParameter("t4"));
int row=pst.executeUpdate();
response.sendRedirect("index.html");
}catch(Exception e){out.print(e);}
%>
</body>
</html>
Newjsp1.jsp-
<%--
Document : newjsp1
Created on : Sep 25, 2023, 2:43:10 PM
Author : admin
--%>
Practical No.:10
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.
Sql query-
Index.html-
<html>
<body>
><br>
</form>
</body>
</html>
Update.jsp-
<%@page contentType="text/html" import="java.sql.*" %>
<html><body>
<%
String
eno=request.getParameter("txtEno
"); String
name=request.getParameter("txtNa
request.getParameter("txtAge");
String sal =
request.getParameter("txtSal");
try{
Class.forName("com.mysql.jd
bc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/emdb");
ResultSet rs =
stmt.executeQuery();
if(rs.next()){
pst2.setString(1, age);
pst2.setString(2, eno);
pst1.executeUpdate();
pst2.executeUpdate();
else{
}catch(Exception e){out.println(e);}
%></body></html>