Session Tracking in Servlets
Prof. Neeraj Bhargava
Kapil Chauhan
Department of Computer Science
School of Engineering & Systems Sciences
MDS University, Ajmer
Introduction
 Session simply means a particular interval of time.
 Session Tracking is a way to maintain state (data) of
an user. It is also known as session management in
servlet.
Cont..
 Http protocol is a stateless so we need to maintain
state using session tracking techniques.
 Each time user requests to the server, server treats the
request as the new request.
 So we need to maintain the state of an user to
recognize to particular user.
Cont..
 HTTP is a "stateless" protocol which means each time
a client retrieves a Web page, the client opens a
separate connection to the Web server and the server
automatically does not keep any record of previous
client request.
 HTTP is stateless that means each request is
considered as the new request.
Session Tracking
Session Tracking Techniques
 There are four techniques used in Session tracking:
 Cookies
 Hidden Form Field
 URL Rewriting
 HttpSession
Method & Description
 public Object getAttribute(String name)
 This method returns the object bound with the
specified name in this session, or null if no object is
bound under the name.
 public Enumeration getAttributeNames()
 This method returns an Enumeration of String objects
containing the names of all the objects bound to this
session.
Cont..
 public int getMaxInactiveInterval()
 This method returns the maximum time interval
(seconds), that the servlet container will keep the
session open between client accesses.
 public void invalidate()
 This method invalidates this session and unbinds any
objects bound to it.
 public boolean isNew(
 This method returns true if the client does not yet
know about the session or if the client chooses not to
join the session.
Example HttpSession session = request.getSession(false);
 try {
 response.setContentType("text/html");
 PrintWriter writer = response.getWriter();
 writer.println("<html><body>");
 // If you are not in a session - you are not logged in
 if (session == null) {
 writer.println("<p>You are not logged in</p>");
 } else {
 writer.println("Thank you, you are already logged in");
 writer.println("Here is the data in your session");
 Enumeration names = session.getAttributeNames();
 while (names.hasMoreElements()) {
 String name = (String) names.nextElement();
 Object value = session.getAttribute(name);
 writer.println("<p>name=" + name + " value=" + value + "</p>");
 }
 }
Assignment
 Explain Session Tracking in servlet with suitable
example.

Session Tracking in servlets

  • 1.
    Session Tracking inServlets Prof. Neeraj Bhargava Kapil Chauhan Department of Computer Science School of Engineering & Systems Sciences MDS University, Ajmer
  • 2.
    Introduction  Session simplymeans a particular interval of time.  Session Tracking is a way to maintain state (data) of an user. It is also known as session management in servlet.
  • 3.
    Cont..  Http protocolis a stateless so we need to maintain state using session tracking techniques.  Each time user requests to the server, server treats the request as the new request.  So we need to maintain the state of an user to recognize to particular user.
  • 4.
    Cont..  HTTP isa "stateless" protocol which means each time a client retrieves a Web page, the client opens a separate connection to the Web server and the server automatically does not keep any record of previous client request.  HTTP is stateless that means each request is considered as the new request.
  • 5.
  • 6.
    Session Tracking Techniques There are four techniques used in Session tracking:  Cookies  Hidden Form Field  URL Rewriting  HttpSession
  • 7.
    Method & Description public Object getAttribute(String name)  This method returns the object bound with the specified name in this session, or null if no object is bound under the name.  public Enumeration getAttributeNames()  This method returns an Enumeration of String objects containing the names of all the objects bound to this session.
  • 8.
    Cont..  public intgetMaxInactiveInterval()  This method returns the maximum time interval (seconds), that the servlet container will keep the session open between client accesses.  public void invalidate()  This method invalidates this session and unbinds any objects bound to it.  public boolean isNew(  This method returns true if the client does not yet know about the session or if the client chooses not to join the session.
  • 9.
    Example HttpSession session= request.getSession(false);  try {  response.setContentType("text/html");  PrintWriter writer = response.getWriter();  writer.println("<html><body>");  // If you are not in a session - you are not logged in  if (session == null) {  writer.println("<p>You are not logged in</p>");  } else {  writer.println("Thank you, you are already logged in");  writer.println("Here is the data in your session");  Enumeration names = session.getAttributeNames();  while (names.hasMoreElements()) {  String name = (String) names.nextElement();  Object value = session.getAttribute(name);  writer.println("<p>name=" + name + " value=" + value + "</p>");  }  }
  • 10.
    Assignment  Explain SessionTracking in servlet with suitable example.