Using Cookies
Example 8:
Session Tracking
·
Say you visit a shopping site today and searched
for a book. And after a while you again visit the site, where you can notice
what are the items that you searched/viewed in last attempt. Thus this is a
very simple scenario that shows that your session is tracked
·
HTTPServletRequest's getSession() is the one
that aids to achieve this. HTTPSession is the out value from this method
·
getAttribute() and setAttribute() are the
methods used to track across requests
Let’s see the example that explains the hands on for above
scenario
o
First Attempt : search for 'Mobile'
o
Second Attempt : search for 'Tablet' (Here you
can see what you searched in first attempt)
o
Third Attempt : search for 'Data Card' (Here you
can see what you searched in second attempt)
Example 8:
·
Create following files in a folder (Please check
next slide for code)
o HTMLFile
index. html Contains html that allows user to
enter details
o Java
File ReadCookie.java Contains logic to read/write
session content
o DD
web.xml Contains deployment
details. Deployment Descriptor
·
Compile the Java File as you did in earlier
example
·
Create below structure with files that you
created above
o Folder
"SessionTracking" contains
o I__
"WEB-INF" Folder and index.html
o I__"WEB-INF"
Folder contains "classes" Folder and web.xml File
o I__"classes"
Folder contains "SessionTracking.class" File
·
Deploy the SessionTracking folder inside webapps
folder of Tomcat
·
Bounce the Server
·
Send request using
"http://localhost:8550/SessionTracking/index.html"
·
index.html
<html>
<body>
<center> <hl
align="center">Welcome to Online Shopping...</h1>
<form
name="ItemSearchForm" action="ItemSearch.do">
<table>
<tr>
<td><B>Enter the
Item to Search: </td>
<td><input
type=textbox name="ItemName" size="25"
value=""></td>
</tr>
</table>
<input type=submit
value="Start Search">
</form>
</body>
</html>
·
SessionTracking.java:
This servlet that reads/writes content in session
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionTracking
extends HttpServlet {
public void
doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
HttpSession hs =
request.getSession(true);
response.setContentType("text/html");
PrintWriter pw =
response.getWriter();
pw.print("<B>");
String lastsearched =
(String)hs.getAttribute("lastitemname");
if(lastsearched != null)
pw.print("Thanks for revising our site. Last you searched for : " +
lastsearched + "<br>");
}
String currentItem =
request.getParameter("ItemName");
pw.println("Current Item
that you are searching : " + currentItem);
hs.setAttribute("lastitemname",
currentItem);
pw.close();
}
}
·
web.xml
o <servlet>
tag maps your servlet class with internal name
o <servlet-mapping>
tag maps internal name with url-pattern that your invoke
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmins:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>sessionservlet</servlet-name>
<servlet-class>SessionTracking</servlet-class>
</servlet>
<servlet-mapping>
<serviet-name>sessionservlet</serviet-name>
<url-pattern>/llemSearch.doqurl-pattern>
</servlet-mapping>
</web-app>
http://localhost:8550/SessionTracking/index.html
- Enter DataCard - Search