Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Servlets II: Session Tracking

Similar presentations


Presentation on theme: "Java Servlets II: Session Tracking"— Presentation transcript:

1 Java Servlets II: Session Tracking
Jagdish Gangolly State University of New York at Albany Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

2 Java Servlets II: Session Tracking
Introduction Methods of Session Tracking Session tracking in Servlets State Management Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

3 Introduction HTTP is a stateless protocol: When a request is made by client, a connection is opened, the server response is sent, and the connection is closed. Consequences: If a transaction requires a sequence of requests/responses, since each request is independent, it is not possible to maintain information regarding the transaction Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

4 Introduction Basic concepts:
Session: A series of requests from a single client is associated with one session State: Associated with each session is a state Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

5 Methods of Session Tracking I
When the initial request is made by the client, the server generates and exchanges a token. 1. URL Rewriting: An Example: a jsp page d=OqZG1m7Nbz51AHCffo0lbVixefzA8OsfH7ObauWHODpZ1RhU5TSS&acty=null CustomerIDxxxxxxxxxx|Jagdishwww.delta.com/ * (COOKIE) Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

6 Methods of Session Tracking II
2. Hidden FORM fields: Not used in servlets <INPUT TYPE=“HIDDEN” NAME=“uid” VALUE=“joe”> 3. Cookies: CFID231648www.technologynews.net/ *CFTOKEN www.tech nologynews.net/ 97684* Some good URLs for cookies: Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

7 Methods of Session Tracking III
<HEAD> <TITLE>DoubleClick Inc. Home Page</TITLE> <LINK HREF="dc.css" REL="styleSheet" TYPE="text/css"> <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"> <!-- // hide me var domain = " var cookieName = "defaultRegion"; var today = new Date(); var expireDate = new Date(); expireDate.setTime(today.getTime() *60*60*24*365); var baseURL = " Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

8 Methods of Session Tracking IV
function setCookie(name, value, expires, path, domain) { var oreo = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : ""); document.cookie = oreo; } Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

9 Methods of Session Tracking V
function getCookie() { var myCookie = document.cookie; var prefix = cookieName + "="; var begin = myCookie.indexOf("; " + prefix); if (begin == -1) { begin = myCookie.indexOf(prefix); if (begin != 0) return null; } else begin += 2; var end = myCookie.indexOf(";", begin); if (end == -1) end = myCookie.length; return unescape(myCookie.substring(begin + prefix.length, end)); } </SCRIPT></HEAD> Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

10 Methods of Session Tracking VI
HTTPServletRequest interface provides getSession() method to create a session You can use this method to get an HTTPSession object Since the server does not know if the client browser has not logged off or the browser has been closed without logging off, you can setMaxInactiveInterval() for the HTTPSession object (time-out parameter) The web container maintains an HTTPSession object for each client Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

11 Methods of Session Tracking VII
A Session Lifecycle example Objective: To report on the servlet lifecycle Steps: Import classes If action is “invalidate, then get the session information and Respond that the session has been invalidated else get session information, if session is new then respond that it is a new session, respond with session information. Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

12 Methods of Session Tracking VIII : Import classes
// Import Servlet packages import javax.servlet.*; import javax.servlet.http.*; // Import Java packages import java.io.*; import java.util.Date; Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

13 Methods of Session Tracking IX: SessionLifeCycleServlet
public class SessionLifeCycleServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); if (action != null && action.equals("invalidate")) { get session and respond that the session has been invalidated } else { Get session and prepare header if (session.isNew()) { respond that it is a "New Session." Respond it is an old session and give session info } prepare rest of the HTML response page}} Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

14 Methods of Session Tracking X: get session and respond “invalidated”
HttpSession session = request.getSession(); session.invalidate(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<HEAD><TITLE>Session Life Cycle</TITLE></HEAD>"); out.println("<BODY>"); out.println("<P>Your session has been invalidated.</p>"); String lifeCycleURL = "/session/servlet/lifeCycle"; //String lifeCycleURL = response.encodeURL("/session/servlet/lifeCycle"); out.println("<A HREF=\"" + lifeCycleURL + "?action=newSession\">"); out.println("Create new session</a>"); out.println("</BODY></HTML>"); Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

15 Methods of Session Tracking XI: Get session and prepare header
HttpSession session = request.getSession(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"); out.println("<HEAD><TITLE>Session Life Cycle</TITLE></HEAD>"); out.println("<BODY BGCOLOR=\"#FFFFFF\">"); out.println("<H1>SessionLifecycle</CENTER></H1>"); out.print("<BR>Session Status: "); Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

16 Methods of Session Tracking XII: respond that it is a "New Session."
out.println("New Session."); Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

17 Methods of Session Tracking XIII: Respond old session and give session info
out.println("<BR>Session ID: "); out.println(session.getId()); out.println("<BR>Creation Time: "); out.println(new Date(session.getCreationTime())); out.println("<BR>Last Accessed Time: "); out.println(new Date(session.getLastAccessedTime())); out.println("<BR>Maximum Inactive Interval (seconds): "); out.println(session.getMaxInactiveInterval()); Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

18 Methods of Session Tracking XIV: prepare rest of the HTML response page
String lifeCycleURL = "/session/servlet/lifeCycle"; //String lifeCycleURL = response.encodeURL("/session/servlet/lifeCycle"); out.print("<BR><A HREF=\"" + lifeCycleURL + "?action=invalidate\">"); out.println("Invalidate the session</A></TD></TR>"); out.print("<BR><A HREF=\"" + lifeCycleURL + "\">"); out.println("Reload this page</A>"); out.println("</BODY></HTML>"); out.close(); Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018

19 State Management: Methods
HTTPSession Public object getAttribute(String name) Public Enumeration getAttributeNames(String name) Public setAttribute(String name, Object attribute) Public void removeAttribute(String name) Acc 683, Spring 2001 Jagdish S. Gangolly 11/11/2018


Download ppt "Java Servlets II: Session Tracking"

Similar presentations


Ads by Google