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
![javax.servlet.http Package,Interfaces of javax.servlet.http package,Classes of javax.servlet.http package,Method of HttpServletRequest Interface,Methods of HttpServletResponse Interface,Methods of HttpSession Interface,Methods of HttpSessionBindingListener Interface,Reading Initialization Parameters,Reading Servlet Parameters,Methods of ServletRequest Interface,Methods of ServletResponse Interface,ServletResponse Interface,ServletRequest Interface,Methods of Servlet Interface,Methods of ServletConfig Interface,Methods of ServletContext Interface,ServletContext Interface,ServletConfig Interface,Servlet Interface,How to install Tomcat,How to install Tomcat webserver,Instructions to install and configure the Tomcat,steps to install tomcat webserver,process to install tomcat webserver,tomcat webserver installation,tomcat webserver installation steps,tomcat webserver installation steps,estudies4you,Web Technologies,JNTUH R16 Web Technologies Lecture notes pdf,Web Technologies class room notes pdf,Web Technologies notes unit wise,Web Technologies course file,Web Technologies previous questio papers,Web Technologies old question papers,Web Technologies MCQ,jntuh r16 Web Technologies syllabus pdf,Web Technologies study material,examples of tomcat server,example of tomcat server with program,Servlet that checks user details,Interfaces of javax.servlet package,javax.Servlet Package,API to develop Servlet,Handling Http Request & Responses,Using Cookies Session Tracking in web technologies,examples of Using Cookies-Session Tracking,web technologies session tracking,web technologies cookies session tracking](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAYOiTuncyixXIOQDyHZd4KxKVsXHOzG7-b50EtHsQqyE7ZZij3FYhPrF5TafabcfctdQqGRbFKBe7CHZ6YMd8ed7B1-skIqADDUUvWbkMUMXGzXHLzAOHulO1k_AxrWoruwMbX2l98YE/s640/Web+Technologies.jpg)