Practical : 10
Subject : Advanced Java
Aim : Create login form and perform state management using Cookies, HttpSession and URL Rewriting.
index.html Code:
--------------------------------------------------------------------------------
<html>
<head>
<title>Login</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.b1
{
width: 370px;
height: 40px;
margin:10px;
color: white;
background-color: #008CBA;
border: 0px solid grey;
cursor: pointer;
}
.side
{
padding: 10px;
margin: 10px;
width: 370px;
}
h1{color:white;}
.box{margin-top:200px;}
</style>
</head>
<body bgcolor="#222222" align="center">
<div class="box">
<h1> Welcome to Login Portal</h1>
<form action="FirstServlet" method="post">
<input class="side" type="text" name="userName" placeholder="Username"/><br/>
<input class="side" type="password" name="password" placeholder="Password"/><br/>
<input class="b1" type="submit" value="Login"/>
</form>
</div>
</body>
</html>
<html>
<head>
<title>Login</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.b1
{
width: 370px;
height: 40px;
margin:10px;
color: white;
background-color: #008CBA;
border: 0px solid grey;
cursor: pointer;
}
.side
{
padding: 10px;
margin: 10px;
width: 370px;
}
h1{color:white;}
.box{margin-top:200px;}
</style>
</head>
<body bgcolor="#222222" align="center">
<div class="box">
<h1> Welcome to Login Portal</h1>
<form action="FirstServlet" method="post">
<input class="side" type="text" name="userName" placeholder="Username"/><br/>
<input class="side" type="password" name="password" placeholder="Password"/><br/>
<input class="b1" type="submit" value="Login"/>
</form>
</div>
</body>
</html>
--------------------------------------------------------------------------------
FirstServlet.java Code:
--------------------------------------------------------------------------------
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name=request.getParameter("userName");
String pwd=request.getParameter("password");
if(pwd.equals("birju") && name.equals("admin"))
{
out.print("<h1>Welcome "+name+"</h1>");
Cookie ck=new Cookie("uname",name);//creating cookie object
response.addCookie(ck);//adding cookie in the response
out.print("<b>Cookie has been generated for this session<br></b>");
out.print("<b>Click on button to view Cookie...</b>");
//creating submit button
out.print("<form action='SecondServlet' method='post'>");
out.print("<br><input type='submit' value='go'>");
out.print("</form>");
}
else
{
out.println("Incorrect Username or Password!!!");
}
out.close();
}catch(Exception e){System.out.println(e);}
}
}
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name=request.getParameter("userName");
String pwd=request.getParameter("password");
if(pwd.equals("birju") && name.equals("admin"))
{
out.print("<h1>Welcome "+name+"</h1>");
Cookie ck=new Cookie("uname",name);//creating cookie object
response.addCookie(ck);//adding cookie in the response
out.print("<b>Cookie has been generated for this session<br></b>");
out.print("<b>Click on button to view Cookie...</b>");
//creating submit button
out.print("<form action='SecondServlet' method='post'>");
out.print("<br><input type='submit' value='go'>");
out.print("</form>");
}
else
{
out.println("Incorrect Username or Password!!!");
}
out.close();
}catch(Exception e){System.out.println(e);}
}
}
--------------------------------------------------------------------------------
SecondServlet.java Code:
--------------------------------------------------------------------------------
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;
public class SecondServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie ck[]=request.getCookies();
out.print("Value stored in Cookie : "+ck[0].getValue());
out.close();
}catch(Exception e){System.out.println(e);}
}
}
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;
public class SecondServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie ck[]=request.getCookies();
out.print("Value stored in Cookie : "+ck[0].getValue());
out.close();
}catch(Exception e){System.out.println(e);}
}
}
--------------------------------------------------------------------------------
web.xml Code:
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SecondServlet</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/FirstServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SecondServlet</servlet-name>
<url-pattern>/SecondServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
--------------------------------------------------------------------------------
Output:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SecondServlet</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/FirstServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SecondServlet</servlet-name>
<url-pattern>/SecondServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Output:
index.html
FirstServlet
SecondServlet
Sign up here with your email
ConversionConversion EmoticonEmoticon