Class8

675
-1

Published on

Hariprasanna V (9843824677)

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
675
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Class8

  1. 1. Java Servlets II: Session Tracking Jagdish Gangolly State University of New York at Albany
  2. 2. Java Servlets II: Session Tracking <ul><li>Introduction </li></ul><ul><li>Methods of Session Tracking </li></ul><ul><li>Session tracking in Servlets </li></ul><ul><li>State Management </li></ul>
  3. 3. Introduction <ul><li>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. </li></ul><ul><ul><li>Consequences: </li></ul></ul><ul><ul><ul><li>If a transaction requires a sequence of requests/responses, since each request is independent, it is not possible to maintain information regarding the transaction </li></ul></ul></ul>
  4. 4. Introduction <ul><li>Basic concepts: </li></ul><ul><ul><li>Session: A series of requests from a single client is associated with one session </li></ul></ul><ul><ul><li>State: Associated with each session is a stat e </li></ul></ul>
  5. 5. Methods of Session Tracking I <ul><li>When the initial request is made by the client, </li></ul><ul><li>the server generates and exchanges a token. </li></ul><ul><li>1. URL Rewriting: </li></ul><ul><ul><li>An Example: a jsp page </li></ul></ul><ul><ul><li>http://www.delta.com/home/index.jsp?acty=null&sessioni </li></ul></ul><ul><ul><li>d=OqZG1m7Nbz51AHCffo0lbVixefzA8OsfH7ObauWHODpZ1RhU5TSS&acty=null </li></ul></ul><ul><li>https://www.delta.com/travel/reservations/itineraries/details/itin_details.jsp?cmd=detail&pnr=XNYY4B&flightnum=0941&flightdate=27MAR01&origin=ALB&destination=ATL&sessionid=OqZORQ5f2lM1uo9l2juGqTWMd0hrdQRKroIqAWe6qFvE4P1hlR68 </li></ul><ul><li>CustomerIDxxxxxxxxxx|Jagdishwww.delta.com/0213647872029623180188817971229402903* (COOKIE) </li></ul>
  6. 6. Methods of Session Tracking II <ul><li>2. Hidden FORM fields: Not used in servlets </li></ul><ul><li><INPUT TYPE=“HIDDEN” NAME=“uid” VALUE=“joe”> </li></ul><ul><li>3. Cookies: </li></ul><ul><li>CFID231648www.technologynews.net/03546759168320 </li></ul><ul><li>88942299290753629397684*CFTOKEN67770976www.tech </li></ul><ul><li>nologynews.net/03546759168320889422993707536293 </li></ul><ul><li>97684* </li></ul><ul><li>Some good URLs for cookies: </li></ul><ul><li>http://www.virtual.net/Projects/Cookies/Cookie_BOF. html </li></ul><ul><li>http://arctic.org/~dean/cookies </li></ul><ul><li>http://www.cis.ohio-state.edu/htbin/rfc/rfc2109.html </li></ul><ul><li>http://www.lanl.gov/projects/ia/library/bits/bits0697.html </li></ul>
  7. 7. Methods of Session Tracking III <ul><li><HEAD> </li></ul><ul><li><TITLE>DoubleClick Inc. Home Page</TITLE> </li></ul><ul><li><LINK HREF=&quot;dc.css&quot; REL=&quot;styleSheet&quot; TYPE=&quot;text/css&quot;> </li></ul><ul><li><SCRIPT LANGUAGE=&quot;JavaScript&quot; TYPE=&quot;text/javascript&quot;> </li></ul><ul><li><!-- // hide me </li></ul><ul><li>var domain = &quot;www.doubleclick.net&quot;; </li></ul><ul><li>var cookieName = &quot;defaultRegion&quot;; </li></ul><ul><li>var today = new Date(); </li></ul><ul><li>var expireDate = new Date(); </li></ul><ul><li>expireDate.setTime(today.getTime() + 1000*60*60*24*365); </li></ul><ul><li>var baseURL = &quot;http://www.doubleclick.net/&quot;; </li></ul><ul><li>… </li></ul>
  8. 8. Methods of Session Tracking I V <ul><li>… </li></ul><ul><li>function setCookie(name, value, expires, path, domain) { </li></ul><ul><li>var oreo = name + &quot;=&quot; + escape(value) + </li></ul><ul><li>((expires) ? &quot;; expires=&quot; + expires.toGMTString() : &quot;&quot;) + </li></ul><ul><li>((path) ? &quot;; path=&quot; + path : &quot;&quot;) + </li></ul><ul><li>((domain) ? &quot;; domain=&quot; + domain : &quot;&quot;); </li></ul><ul><li>document.cookie = oreo; </li></ul><ul><li>} </li></ul><ul><li>… </li></ul>
  9. 9. Methods of Session Tracking V <ul><li>… </li></ul><ul><li>function getCookie() { </li></ul><ul><li>var myCookie = document.cookie; </li></ul><ul><li>var prefix = cookieName + &quot;=&quot;; </li></ul><ul><li>var begin = myCookie.indexOf(&quot;; &quot; + prefix); </li></ul><ul><li>if (begin == -1) { </li></ul><ul><li>begin = myCookie.indexOf(prefix); </li></ul><ul><li>if (begin != 0) return null; </li></ul><ul><li>} else </li></ul><ul><li>begin += 2; </li></ul><ul><li>var end = myCookie.indexOf(&quot;;&quot;, begin); </li></ul><ul><li>if (end == -1) end = myCookie.length; </li></ul><ul><li>return unescape(myCookie.substring(begin + prefix.length, end)); </li></ul><ul><li>} </li></ul><ul><li>… </li></ul><ul><li></SCRIPT></HEAD> </li></ul>
  10. 10. Methods of Session Tracking VI <ul><li>HTTPServletRequest interface provides getSession() method to create a session </li></ul><ul><li>You can use this method to get an HTTPSession object </li></ul><ul><li>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) </li></ul><ul><li>The web container maintains an HTTPSession object for each client </li></ul>
  11. 11. Methods of Session Tracking VII <ul><li>A Session Lifecycle example </li></ul><ul><li>Objective: To report on the servlet lifecycle </li></ul><ul><li>Steps: </li></ul><ul><ul><li>Import classes </li></ul></ul><ul><ul><li>If action is “invalidate, then </li></ul></ul><ul><ul><li>get the session information and Respond that the session has been invalidated </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>get session information, </li></ul></ul><ul><ul><li>if session is new then </li></ul></ul><ul><ul><li>respond that it is a new session, </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>respond with session information. </li></ul></ul>
  12. 12. Methods of Session Tracking VIII : Import classes <ul><li>// Import Servlet packages </li></ul><ul><li>import javax.servlet.*; </li></ul><ul><li>import javax.servlet.http.*; </li></ul><ul><li>// Import Java packages </li></ul><ul><li>import java.io.*; </li></ul><ul><li>import java.util.Date; </li></ul>
  13. 13. Methods of Session Tracking IX: SessionLifeCycleServlet <ul><li>public class SessionLifeCycleServlet extends HttpServlet </li></ul><ul><li>{ </li></ul><ul><li>protected void doGet(HttpServletRequest request, </li></ul><ul><li>HttpServletResponse response) throws </li></ul><ul><li>ServletException, IOException { </li></ul><ul><li>String action = request.getParameter(&quot;action&quot;); </li></ul><ul><li>if (action != null && action.equals(&quot;invalidate&quot;)) { </li></ul><ul><li>get session and respond that </li></ul><ul><li>the session has been invalidated </li></ul><ul><li>} else { </li></ul><ul><li>Get session and prepare header </li></ul><ul><li>if (session.isNew()) { </li></ul><ul><li>respond that it is a &quot;New Session.&quot; </li></ul><ul><li>} else { </li></ul><ul><li>Respond it is an old session and give session info </li></ul><ul><li>} </li></ul><ul><li>prepare rest of the HTML response page }} </li></ul>
  14. 14. Methods of Session Tracking X: get session and respond “invalidated” <ul><li>HttpSession session = request.getSession(); </li></ul><ul><li>session.invalidate(); </li></ul><ul><li>response.setContentType(&quot;text/html&quot;); </li></ul><ul><li>PrintWriter out = response.getWriter(); </li></ul><ul><li>out.println(&quot;<HTML>&quot;); </li></ul><ul><li>out.println(&quot;<HEAD><TITLE>Session Life Cycle</TITLE></HEAD>&quot;); </li></ul><ul><li>out.println(&quot;<BODY>&quot;); </li></ul><ul><li>out.println(&quot;<P>Your session has been invalidated.</p>&quot;); </li></ul><ul><li>String lifeCycleURL = &quot;/session/servlet/lifeCycle&quot;; </li></ul><ul><li>//String lifeCycleURL = response.encodeURL(&quot;/session/servlet/lifeCycle&quot;); </li></ul><ul><li>out.println(&quot;<A HREF=&quot;&quot; + lifeCycleURL + &quot;?action=newSession&quot;>&quot;); </li></ul><ul><li>out.println(&quot;Create new session</a>&quot;); </li></ul><ul><li>out.println(&quot;</BODY></HTML>&quot;); </li></ul>
  15. 15. Methods of Session Tracking XI: Get session and prepare header <ul><li>HttpSession session = request.getSession(); </li></ul><ul><li>response.setContentType(&quot;text/html&quot;); </li></ul><ul><li>PrintWriter out = response.getWriter(); </li></ul><ul><li>out.println(&quot;<HTML>&quot;); </li></ul><ul><li>out.println(&quot;<META HTTP-EQUIV=&quot;Pragma&quot; </li></ul><ul><li>CONTENT=&quot;no-cache&quot;>&quot;); </li></ul><ul><li>out.println(&quot;<HEAD><TITLE>Session Life </li></ul><ul><li>Cycle</TITLE></HEAD>&quot;); </li></ul><ul><li>out.println(&quot;<BODY BGCOLOR=&quot;#FFFFFF&quot;>&quot;); </li></ul><ul><li>out.println(&quot;<H1>SessionLifecycle</CENTER></H1>&quot;); </li></ul><ul><li>out.print(&quot;<BR>Session Status: &quot;); </li></ul>
  16. 16. Methods of Session Tracking XII: respond that it is a &quot;New Session.&quot; <ul><li>out.println(&quot;New Session.&quot;); </li></ul>
  17. 17. Methods of Session Tracking XIII: Respond old session and give session info <ul><li>out.println(&quot;<BR>Session ID: &quot;); </li></ul><ul><li>out.println(session.getId()); </li></ul><ul><li>out.println(&quot;<BR>Creation Time: &quot;); </li></ul><ul><li>out.println(new Date(session.getCreationTime())); </li></ul><ul><li>out.println(&quot;<BR>Last Accessed Time: &quot;); </li></ul><ul><li>out.println(new Date(session.getLastAccessedTime())); </li></ul><ul><li>out.println(&quot;<BR>Maximum Inactive Interval (seconds): &quot;); </li></ul><ul><li>out.println(session.getMaxInactiveInterval()); </li></ul>
  18. 18. Methods of Session Tracking XI V : prepare rest of the HTML response page <ul><li>String lifeCycleURL = &quot;/session/servlet/lifeCycle&quot;; </li></ul><ul><li>//String lifeCycleURL = response.encodeURL(&quot;/session/servlet/lifeCycle&quot;); </li></ul><ul><li>out.print(&quot;<BR><A HREF=&quot;&quot; + lifeCycleURL + &quot;?action=invalidate&quot;>&quot;); </li></ul><ul><li>out.println(&quot;Invalidate the session</A></TD></TR>&quot;); </li></ul><ul><li>out.print(&quot;<BR><A HREF=&quot;&quot; + lifeCycleURL + &quot;&quot;>&quot;); </li></ul><ul><li>out.println(&quot;Reload this page</A>&quot;); </li></ul><ul><li>out.println(&quot;</BODY></HTML>&quot;); </li></ul><ul><li>out.close(); </li></ul>
  19. 19. State Management: Methods <ul><li>HTTPSession </li></ul><ul><ul><li>Public object getAttribute(String name) </li></ul></ul><ul><ul><li>Public Enumeration getAttributeNames(String name) </li></ul></ul><ul><ul><li>Public setAttribute(String name, Object attribute) </li></ul></ul><ul><ul><li>Public void removeAttribute(String name) </li></ul></ul>

×