JSP - SPLessons

JSP Session Tracking

Home > Lesson > Chapter 15
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JSP Session Tracking

JSP Session Tracking

shape Description

JSP Session Tracking - If a web application is capable of remembering a client data during a session across the multiple requests then that web application is called as a stateful Web application. Even though HTTP is a stateless protocol, web applications should be made as stateful web applications. For this, working with the following session tracking or session management techniques is necessary.

Hidden Form Fields

JSP Session Tracking - A web server can send a hidden HTML structure field alongside a unique session ID as follows. [java]<input type="hidden" name="sessionid" value="54321">[/java] at the point when the form is presented, the predetermined name and value are consequently incorporated into the GET or POST information. Every time when web program sends request back, then session_id value can be utilized to monitor distinctive web programs.

Cookies

JSP Session Tracking - A webserver can relegate a one of a kind session ID as a treat to every web browser and for resulting requests from the client they can be perceived utilizing the got cookie. This may not be a successful way on the grounds that numerous time program does not bolster a cookie.

URL Rewriting

JSP Session Tracking - One can add some additional information on the end of every URL that distinguishes the session, and the server can relate that session identifier with information it has put away about that session. URL Rewriting is a superior approach to keep up sessions and works for the programs when they don't support cookies yet here downside is that you would have produce each URL powerfully to allocate a session ID however page is basic static HTML page.

Session Object

JSP Session Tracking - JSP makes utilization of servlet gave HttpSession Interface which gives an approach to distinguish a client crosswise over more than one page request or visit to a Web webpage and to store data about that client. Of course, JSPs have session tracking empowered and another HttpSession object is instantiated for each new client consequently. Incapacitating session following requires unequivocally turning it off by setting the page order session credit to false as follows. [java]<%@ page session="false" %>[/java] Session tracking is all about making web application as a stateful web application by remembering client data across the multiple requests during a session. Following are the RealTime Implementation of session tracking/session management. Following is the servlet session tracking link. Servlet Session Tracking

shape Conceptual Figure

When the client is sending the request, server gives back response where already session will be stored.

shape Example

Following is the session tracking example. index.html [html]<%@ page import="java.io.*,java.util.*" %> <% // 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("SPlesson"); // Check if this is new comer on your web page. if (session.isNew()){ title = "Welcome to SPlessons"; session.setAttribute(userIDKey, userID); session.setAttribute(visitCountKey, visitCount); } visitCount = (Integer)session.getAttribute(visitCountKey); visitCount = visitCount + 1; userID = (String)session.getAttribute(userIDKey); session.setAttribute(visitCountKey, visitCount); %> <html> <head> <title>Session Tracking</title> </head> <body bgcolor ="skyblue"> <center> <img src="E:/splessons.png"></br> <h1>Session Tracking</h1> </center> <table border="1" align="center"> <tr bgcolor="#949494"> <th>Session info</th> <th>Value</th> </tr> <tr> <td>id</td> <td><% out.print( session.getId()); %></td> </tr> <tr> <td>Creation Time</td> <td><% out.print(createTime); %></td> </tr> <tr> <td>Time of Last Access</td> <td><% out.print(lastAccessTime); %></td> </tr> <tr> <td>User ID</td> <td><% out.print(userID); %></td> </tr> <tr> <td>Number of visits</td> <td><% out.print(visitCount); %></td> </tr> </table> </body> </html>[/html] In this code, when compile the code page will be displayed on he browser, if user refresh the page number of counts will be increased. Where the developer used visitCount. When compile the code following is the code will be displayed.

Summary

shape Key Points

  • In the URL phase developer can add a little data to identify the session like sessionid=123.
  • The web server has the ability to send the hidden HTML data along with the session id.