Home Quickguide Tomcat Session tracking basics
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Session tracking basics

Advertisement
Session tracking is a process that servlets use to maintain state about the series of requests from users across some period of time.

Session Tracking Basics

     

Session Tracking

Session tracking is a process that servlets use to maintain state about the series of requests from users across some period of time. In session tracking client first make a request for any servlet, container receives the request and generate a unique session track ID and gives it back to the client along with the response. 

This session tracking ID gets stored on the client machine. when the client request again  for the same request session, then the browser looks for that ID and sends it to the server.  In this case container find the Id and sends back the response to the client. 

Session Tracking can be done in three ways: 
1.Hidden Form Fields
2.URL Rewriting
3.Cookies

Hidden form field is sent back to the server when the form is submitted. In hidden form fields the html entry will be like this : <input type ="hidden" name = "name" value="">. This means that when you submit the form, the specified name and value will be get included in get or post method. Here the session ID information would be embedded within the form as a hidden field and submitted with the Http POST command.

URL Rewriting can be used in place where we don't want to use cookies. This is used to maintain the session. In case the browser sends a request then it is always interpreted as a new request because http protocol is a stateless protocol as it is not persistent. 

In the case of session tracking cookies are a way for a server to send some message to a client to store, and for the server to later retrieve its data from that client. Servlets send cookies to clients by adding fields to HTTP response headers. Clients automatically return cookies by adding fields to HTTP request headers.

The code of the program is given below:

package myservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class ShowSession extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Session Tracking Example";
HttpSession session = request.getSession(true);
String heading;
Integer count = (Integer)session.getAttribute("count");
if (count == null) {
count = new Integer(0);
heading = "Session Tracking Example";
}
else {
heading = "Session Tracking Example";
count = new Integer(count.intValue() + 1);
}
session.setAttribute("count", count);
out.println("</html><head><title>"+title+"</title></head><body>" +
"<h1 align=\"\">" + heading + "</H1>\n" +
"<h2></h2>\n" +
"<table border=1 align=\"\">\n" +
"<tr>"+"<TR bgcolor=\"#9999966\">\n"+
"<th>SessionTracking</th>"+

"<th>Time period</th>" +
"</tr>" +
"<tr>" +
"<td>ID</td>" +
"<td>" + session.getId() + "</td>" +
"</tr>" +
"<tr>" +
"<td>Current Time</td>" +
" <td>" + new Date(session.getCreationTime()) + "</td>" +
"</tr>" +
"<tr>" +
"<td>Time of Last Access</td>" +
" <td>" + new Date(session.getLastAccessedTime()) + "</td>" +
"</tr>" +
"<tr>" +
"<td>Number of Previous Accesses</td>" +
" <td>" + count + "</td>" +
"</tr>"+
"</table>" +
"</body></html>");

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}


web.xml file for this program: 

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
version="2.5">
<description>
Session tracking Example:
</description>
<display-name>Session tracking Example</display-name>
<context-param>
<param-name>User</param-name>
<param-value>RoseIndia</param-value>
</context-param>
<servlet-name>ShowSession</servlet-name>
<servlet-class>myservlets.ShowSession</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowSession</servlet-name>
<url-pattern>/ShowSession</url-pattern>
</servlet-mapping>
</web-app>

The output of the program is given below:

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 8, 2008

Related Tutorials

Ask Questions?    Discuss: Session tracking basics  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com