



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Session simply means a particular interval of time.Session Tracking is a way to maintain state (data) of a user. It is also known as session management in servlet.Http is a stateless protocol that means each request is considered as the new request. So the states are maintained using various 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 a user to recognize to particular user. Session Tracking Techniques There are four techniques used in Session tracking:Cookies, Hidden Form Field, URL Rewriting and HttpSession Interface Cookies A cookie is a small piece of information that persist between the multiple client requests.In HTTP each request is considered as a new request even if two requests are issued by the same user.A cookie is added with response from the servlet which serves as an identifier to the user. This is done when the first request is made.So cookie is stored in the cache of the browser. After that if any request is sent by the user, the stored cookie is added with request by default. Thus, the server recognizes the user as the old user.
lues Example: This entry means that, when the form is submitted, the specified name and value are automatically included in the GET or POST data. Figure 3.7 Hidden Form field Advantage of Hidden Form Field
import java.io.;import javax.servlet.;import javax.servlet.http.; import java.util.; public class SessionTrack extends HttpServlet { Methods of HttpSession interface Method Description public Object getAttribute(String name) returns the object bound with the specified name in this session, or null if no object is bound under the name. public Enumeration getAttributeNames() returns an Enumeration of String objects containing the names of all the objects bound to this session. public long getCreationTime() returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT. public String getId() returns a string containing the unique identifier assigned to this session. public long getLastAccessedTime() returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT. public int getMaxInactiveInterval() returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses. public void invalidate() invalidates this session and unbinds any objects bound to it. public boolean isNew() returns^ true^ if^ the^ client^ does^ not^ yet^ know^ about^ the^ session or if the client chooses not to join the session. public void removeAttribute(String name) removes the object bound with the specified name from this session. public void setAttribute(String name, Object value) binds an object to this session, using the name specified. public void setMaxInactiveInterval(int interval) specifies the time, in seconds, between client requests before the servlet container will invalidate this session. Session tracking
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); //session id is created Date createTime = new Date(session.getCreationTime()); // Get session creation time. Date lastAccessTime = new Date(session.getLastAccessedTime()); // Get last access time of this web page. String title = "Welcome Back to my website"; Integer visitCount = new Integer(0); String visitCountKey = new String("visitCount"); String userIDKey = new String("userID"); String userID = new String("ABCD"); // Check if this is new comer on your web page. if (session.isNew()){ title = "Welcome to my website"; session.setAttribute(userIDKey, userID); } else { visitCount = (Integer)session.getAttribute(visitCountKey); visitCount = visitCount + 1; userID = (String)session.getAttribute(userIDKey); } session.setAttribute(visitCountKey, visitCount); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("\n" + "Session info value \n" + "\n" + " \n" + "id \n" + " " + session.getId() + " \n" + " \n" + "Creation Time \n" + " " + createTime + " \n" +” \n" + "Time of Last Access \n" + " " + lastAccessTime + " \n" + " User