• HTTP protocol and web server are stateless, i.e., for web server, every request is a new request to process, and they can’t identify whether it is coming from the same client or a new client.
• Sometimes, in web applications, we need to identify the client and process the request as per the client.
Ex: In a shopping cart application, we should know
o Who is sending the request?
o On which card has the item to be added?
o Who is sending the place-order request so that it can charge the correct amount to the correct client?
• A session is a period of time where the user sends multiple requests and receives multiple responses.
• In multiple requests, the user may send data that should be stored and managed on the server.
• Sessions will be used to store client-specific data. A session is a conversional state between clients and server.
• In the user session, you have to do two things.
o Identify the client.
o Managed the conversational state (client-specific data).
• To identify the client as new or old, the client uses a session ID.
• To store client conversational state or data,you have to use the HttpSession object.
Using session to manage client specific data.
You can use the following method with the HttpServletRequest object to access the HttpSession object:
HttpSession sess=request.getSession();
HttpSession sess=request.getSession(boolean);
Class HttpServletRequestImpl Implements HttpServletRequest{
public HttpSession getSession(){
• Check whether the incoming request contains the cookies with the same JESSIONID or not.
• If the incoming request contains cookies with the name JESSIONID, then the following task will be performed:
collects the value of the cookie, which is the session ID.
Picks the session object related to this sessionId.
Returns these existing session objects.
• If the incoming request does not contain the cookie with the name JESSIONID, the following task will be performed:
Create a session object.
Generates a unique session id.
Stores the session ID in the session object.
Create the cookie with the name JESSIONID and the value client session ID.
Add the cookie to the response object.
Returns this new session object.
}
}
• There are four session management techniques:
o HttpSession
o Cookies
o Url-Rewriting
o Hidden Fields
• You can use the following to sore the client conversational data:
o HttpSession(***)
o Cookies
• You can use the following to carry the session ID:
o Cookies(***)
o Url_rewriting(***)
o Hidden Fields
Files Required for program
1. Index.jsp
2. Showcart.jsp
3. Showbooks.jsp
4. placeorder.jsp
5. Web.xml
6. AddToCartServlet.java
7. showCartServlet.java
8. RemoveFromCart.java
1. Index.jsp
<%@ page language="java"contentType="text/html; charset=ISO8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>JTC Bookstore</h1>
<h2>Book Search</h2>
<form action="searchbooks.jtc"
method="post">
<table>
<tr>
<td>
<h2>Select Category</h2>
</td>
</tr>
<tr>
<td>
<select name="category">
<option value="java">java</option>
<option value="Testing">Testing</option>
<option value=".NET">.Net</option>
<option value="SAP">SAP</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="SearchBooks"/></td>
</tr>
</table>
</form>
</center>
</body>
</html>
2. showcart.jsp
<%@page import="java.util.*"%>
<%@ page language="java"contentType="text/html; charset=ISO8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>JTC BookStore</h1>
<h2>Book Search</h2>
</center>
<%Object
object=request.getAttribute("MSG");
if(object!=null){
%>
<br>
<center>
<font color="red"
size="6"><%=object%></font>
</center>
<%
}else{
object=request.getAttribute("CAR
T");
ArrayList<String>
blist=(ArrayList<String>)object;
for(String bnm:blist){
%>
<form action="removeformcart.jtc"
method="post">
<input type="hidden" name="bname"
value="<%=bnm%>"/>
<font size="5"><%=bnm%><input
type="submit" value="Remove From
Cart"/></font>
</form>
<%
}
%>
<br>
<center>
<a href="Placeorder.jsp">PLACE
ORDER</a>
</center>
<%
}
%>
<center>
<br><a href="showbooks.jsp">ADD TO
CART</a>
</center>
</body>
</html>
3. showbooks.jsp
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html;charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>JTC Bookstore</h1>
<h2>Book Search</h2>
<font color="green" size="6" >${ADDED }</font>
</center>
<br>
<%Object obj=request.getAttribute("MSG");
if(obj!=null){
%>
<br>
<center>
<font color="red" size="6">
<%=obj %>
</font>
<br><a href="index.jsp">GO TO SEARCH PAGE</a>
</center>
<%
}else{
obj=session.getAttribute("BOOKS");
ArrayList<String>
blist=(ArrayList<String>)obj;
for(String bnm:blist){
%>
<form action="addtocart.jtc" method="post">
<input type="hidden" name="name" value="<%=bnm%>"/>
<font size="5"><%=bnm %>
<input type="submit" value="ADD TO CART"/>
</font>
</form>
<%
}
%>
<br>
<form action="showcart.jtc">
<input type="submit" value="SHOW CART">
</form>
<%
}
%>
</body>
</html>
4. placeorder.jsp
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html;charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>Bookstore</h1>
<h2>Book Search</h2>
<h1>your Order has been placed successfully</h1>
<%
session.invalidate();
%>
<br>
<a href="index.jsp">GO TO SEARCH PAGE</a>
</center>
</body>
</html>
5. Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Jtc18</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SearhBooks</servlet-name>
<servletclass>
com.jtcindia.servlets.SearchBooksServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SearhBooks</servlet-name>
<url-pattern>/searchbooks.jtc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>addToCartServlet</servlet-name>
<servletclass>
com.jtcindia.servlets.AddToCartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addToCartServlet</servlet-name>
<url-pattern>/addtocart.jtc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>showCartServlet</servlet-name>
<servletclass>com.jtcindia.servlets.ShowCartServlet</servletclass>
</servlet>
<servlet-mapping>
<servlet-name>showCartServlet</servlet-name>
<url-pattern>/showcart.jtc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>removeFormCart</servlet-name>
<servletclass>
com.jtcindia.servlets.RemoveFromCartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>removeFormCart</servlet-name>
<url-pattern>/removeformcart.jtc</url-pattern>
</servlet-mapping>
</web-app>
6. AddToCartServlet.java
package com.jtcindia.servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AddToCartServlet extends HttpServlet {
protected void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException,
IOException {
//Accessing the existing session object
HttpSession sess = req.getSession(false);
//validating session is available or not
if (sess == null) {
req.setAttribute("MSG", "Session is destroyed");
} else {
String bnm = req.getParameter("bname");
//Adding the client selected book to session
sess.setAttribute(bnm, bnm);
req.setAttribute("ADDED", bnm + "is added to cart");
}
RequestDispatcher
rd = req.getRequestDispatcher("showbooks.jsp");
rd.forward(req, res);
}
}
7. ShowCartServlet.java
package com.jtcindia.servlets;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ShowCartServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession httpSession = req.getSession(false);
if (httpSession == null) {
req.setAttribute("MSG", "Session is destroyed");
RequestDispatcher rd = req.getRequestDispatcher("showbooks.jsp");
rd.forward(req, res);
} else {
Enumeration < String > enms = httpSession.getAttributeNames();
List < String > selectlist = Collections.list(enms);
selectlist.remove("BOOKS");
if (selectlist.size() == 0) {
req.setAttribute("MSG", "No Books Selected");
} else {
req.setAttribute("CART", selectlist);
}
RequestDispatcher rd = req.getRequestDispatcher("ShowCart.jsp");
rd.forward(req, res);
}
}
}
8. RemoveFromCart.java
package com.jtcindia.servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RemoveFromCartServlet extends
HttpServlet {
protected void service(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {
HttpSession httpSession = request.getSession(false);
if (httpSession == null) {
request.setAttribute("MSG", "Session is destroyed");
} else {
String bnm = request.getParameter("bname");
//Removing the client selected book from session
httpSession.removeAttribute(bnm);
}
RequestDispatcher
rd = request.getRequestDispatcher("ShowCart.jsp");
rd.forward(request, response);
}
}