SESSION TRACKING IN
SERVELET
SESSION
SESSION TRACKING
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.
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.
HTTP is stateless that means each request is considered as the new
request. It is shown in the figure given below:
2/15/2020 2
REAL TIME APPLICATIONS OF SESSION
TRACKING(USES)
USES REAL TIME EXAMPLE
• Session is used to store user specific data on the • if a user has logged into his Bank Account and
server. Such as data retrieved from database after a successful login if he wishes to go to the
specific to that user so that it can be used through Funds Transfer page then he would be required to
out the application for that specific user. The Web login again as Funds Transfer would be a login-
protected page and the Web Server doesn't have any
Server doesn't have a built-in way to recognize
built-in support for recognizing if the client
whether the current request is coming from a requesting this page is the one who is already
new client or from a client which has been logged in or if it's coming from a new client. This is
communicating with it for a while now. This just a simple example. We can easily imagine how
happens because every HTTP request is treated as an difficult will it be to develop a Web-Application
altogether new request. without maintaining contextual information about
the clients.
// Import required java libraries CODING EXAMPLE
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
// Extend HttpServlet class
public class SessionTrack extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Create a session object if it is already not created.
HttpSession session = request.getSession(true);
// 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 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);
// 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>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<h2 align = \"center\">Session Infomation</h2>\n" +
"<table border = \"1\" align = \"center\">\n" +
"<tr bgcolor = \"#949494\">\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>" + visitCount + "</td>
</tr>\n" +
"</table>\n" +
"</body>
</html>"
);
}
}
OUTPUT