Sessions are used to track client accesses and interactions. They answer user-access related questions, such as: How does a server know whether a client logged in? How does an on-line store keep tracking items that a user selected in her cart?. In this tutorial, we will see how to handle sessions using servlet.
// Create a session object if it is already not created. HttpSession session = request.getSession(); // Get session creation time. Date createTime = new Date(session.getCreationTime()); // Get last access time of this web page. Date lastAccessTime = new Date(session.getLastAccessedTime()); String heading; Integer accessCount = 0; String accessCountKey = "AccessCountKey"; String userID = "chan"; // Check if this is new comer on your web page. if (session.isNew()){ heading = "Welcome, "+userID; accessCount=0; } else { heading = "Welcome back"; accessCount = (Integer)session.getAttribute(accessCountKey); accessCount = accessCount + 1; } session.setAttribute(accessCountKey, accessCount); //session.invalidate(); // Set response content type response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<html>\n" + "<head><title> Servlet Session tutorial </title></head>\n" + "<body>\n" + "<h1 align=\"center\">" + heading + "</h1>\n" + "<table border=\"1\" align=\"center\">\n" + "<tr>\n" + " <th>Session info</th><th>value</th></tr>\n" + "<tr>\n" + " <td>id</td>\n" + " <td>" + session.getId() + "</td></tr>\n" + "<tr>\n" + " <td>Creation Time</td>\n" + " <td>" + createTime + " </td></tr>\n" + "<tr>\n" + " <td>Time of Last Access</td>\n" + " <td>" + lastAccessTime + " </td></tr>\n" + "<tr>\n" + " <td>User ID</td>\n" + " <td>" + userID + " </td></tr>\n" + "<tr>\n" + " <td>Number of visits</td>\n" + " <td>" + accessCount + "</td></tr>\n" + "</table>\n" + "</body></html>");
import javax.servlet.http.HttpSession; import java.io.PrintWriter; import java.util.Date;
// Create a session object if it is already not created. HttpSession session = request.getSession(); ArrayList<String> previousItems = (ArrayList<String>)session.getAttribute("previousItems"); if (previousItems == null) { previousItems = new ArrayList<String>(); } String newItem = request.getParameter("newItem"); if ((newItem != null) && (!newItem.trim().equals(""))) { previousItems.add(newItem); } session.setAttribute("previousItems", previousItems); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n"; out.println(docType + "<HTML>\n" + "<HEAD><TITLE> List Items </TITLE></HEAD>\n" + "<BODY>\n" + "<H1> List of Item </H1>"); if (previousItems.size() == 0) { out.println("<I>No items</I>"); } else { out.println("<UL>"); for(String item: previousItems) { out.println(" <LI>" + item); } out.println("</UL>"); } out.println("</BODY></HTML>");
import javax.servlet.http.HttpSession; import java.util.ArrayList; import java.io.PrintWriter;