Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Session tracking basics 
 

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

                         

» View all related tutorials
Related Tags: c database security web com rest ant website data io connection user password word uri name using this tab check

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.