Practical-15 Create a web form which processes servlet and demonstrates use of cookies and sessions.
Use of Cookies in Servlet:
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Use of Cookie</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form id="CookiDemo" action="AddCookieServlet" method="POST">
<b>Enter a value for MyCookie:</b>
<input type="text" name="data" size=25 value=""/>
<br><br>
<input type="submit" name="Submit"/>
</form>
</body>
</html>
<html>
<head>
<title>Use of Cookie</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form id="CookiDemo" action="AddCookieServlet" method="POST">
<b>Enter a value for MyCookie:</b>
<input type="text" name="data" size=25 value=""/>
<br><br>
<input type="submit" name="Submit"/>
</form>
</body>
</html>
Create Two Servlet File with name AddCookieServlet and GetCookieServlet
1. AddCookieServlet.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AddCookieServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String data=request.getParameter("data");
Cookie cookie=new Cookie("MyCookie",data);
response.addCookie(cookie);
out.println("<b>Cookie Created: MyCookie=");
out.println(data);
out.println("<a href='GetCookieServlet'> Click to access</a>");
out.close();
}
}
2. GetCookieServlet.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetCookieServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie[] cookies=request.getCookies();
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<b>");
for(int i=0;i<cookies.length;i++)
{
String name=cookies[i].getName();
String value=cookies[i].getValue();
out.println("name="+name+" value="+value);
}
out.close();
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" 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_4_0.xsd">
<servlet>
<servlet-name>AddCookieServlet</servlet-name>
<servlet-class>AddCookieServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>GetCookieServlet</servlet-name>
<servlet-class>GetCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddCookieServlet</servlet-name>
<url-pattern>/AddCookieServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GetCookieServlet</servlet-name>
<url-pattern>/GetCookieServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Use of Session in Servlet:
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Use of Session</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form id="SessionDemo" action="SessionServlet" method="GET">
<label>Username:</label>
<input type="text" name="name"/>
<br><br>
<input type="submit" name="Submit"/>
</form>
</body>
</html>
Create Two Servlet File with name SessionServlet and SessionTrack
1. SessionServlet.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String n=request.getParameter("name");
out.print("Welcome: "+n);
HttpSession session=request.getSession();
session.setAttribute("uname", n);
out.print("<a href='SessionTrack'> Check Session Tracking</a>");
out.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
2. SessionTrack.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionTrack extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession(false);
String n=(String)session.getAttribute("uname");
out.print("Hello "+n);
out.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" 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_4_0.xsd">
<servlet>
<servlet-name>SessionServlet</servlet-name>
<servlet-class>SessionServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SessionTrack</servlet-name>
<servlet-class>SessionTrack</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionServlet</servlet-name>
<url-pattern>/SessionServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SessionTrack</servlet-name>
<url-pattern>/SessionTrack</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
15
</session-timeout>
</session-config>
</web-app>
Comments
Post a Comment