Usually the following four steps are followed while using sessions in servlets.
1. Accessing the Session object associated with the current request: In this step, invoke the getSession () method of the HttpServletRequest to return the HttpSession object.
2. Store objects into a Session object using setAttribute () method.
3. Discard session data if necessary-Call removeAttribute () to discard a specific value. Call invalidate () to discard the entire session.
Now let us consider a program where we create a servlet that uses a session to keep track of how many times a client has visited the webpage. In addition, the servlet also displays the information related to the session such sessionID, creation time, last accessed time.
import java.io.*;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionSevlet extends HttpServlet
{
public void doGet (HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException)
{
response.setContentType (“text/html”);
PrintWriter out = response.getWriter ();
HttpSession session = request.getSession ();
Integer count = (Integer) session.getAttribute (“count”);
if (count == null)
count = new Integer (l);
else
count =new Integer (count.intValue () +1);
session.setAttribute (“count”, count);
out.println (“<html> <head> <title> Session Demo </title> </head>”);
out.println (“<body> <h2> Session Information </h2>”);
out.println (“No. of times you have visited this page =” +count);
out.println (“<h3> Current session details: </h3>”);
out.println (“Session id:” +session.getId () +”<br/>”);
out.println (“New session:” +session.isNew () +”<br/>”);
out.println (“Timeout:” +session.getMaxInactiveInterval () +”<br/>”);
out.println (“Creation time:” +new Date (session.getCreationTime () +”<br/>”);
out.println (“Last access time:” +new Date (session.getLastAccessedTime ()
+”<br/>”);
out.println (“</body> </html>”);
}
}
Explanation : In the above program, the statement
HttpSession session = request.getSession ();
returns a session associated with the user making the request. If the session does not exists it creates a new session. This is the first step required for creating and tracking HttpSession. Then the statement,
Integer count = (Integer) session.getAttribute (“count”);
returns a value associated with the session variable count. Since the getAttribute () method returns an Object type cast to it, the appropriate type before calling any methods on it. In our case, we have type cast it to Integer object because we want to display the value of count as an integer. The next statements,
if (count == null)
count = new Integer(l);
else
count = new Integer(count.intvalue () +l);
checks whether count object is null or not. It is null (i.e. there is no such object), the servlet starts a new count. Otherwise, it replaces the Integer with a new Integer whose value has been incremented by 1.
Finally, the servlet generates the html code that displays the number of times the client has visited the page along with other session information.