08 Session Tracking
08 Session Tracking
Session Tracking S i T ki
Originals of Slides and Source Code for Examples: http://courses.coreservlets.com/Course-Materials/csajsp2.html
Customized Java EE Training: http://courses.coreservlets.com/
3
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Servlets, JSP, JSF 2, Ajax (with jQuery, Dojo, Prototype, Ext-JS, etc.), GWT, Java 6, SOAP-based and RESTful Web Services, Spring, Hibernate/JPA, Android, Hibernate/JPA Android and customized combinations of topics topics.
For live Java EE training, please see training courses at http://courses.coreservlets.com/. http://courses coreservlets com/
Taught by the author of Core Servlets and JSP, More Servlets and JSP and this tutorial. Available at public JSP, tutorial venues, or customized versions can be held on-site at your Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. organization. Contact [email protected] for details. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Agenda
5
Implementing session tracking from scratch Using basic session tracking Understanding the session-tracking API Differentiating between server and browser Diff ti ti b t db sessions Encoding URLs Tracking user access counts Accumulating user purchases Implementing a shopping cart Building an online store
Overview
Customized Java EE Training: http://courses.coreservlets.com/
6
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Still to be done:
Extracting cookie that stores session identifier Setting appropriate expiration time for cookie Associating the hash tables with each request Generating the unique session identifiers
Advantage
Works even if cookies are disabled or unsupported
Di d Disadvantages t
Must encode all URLs that refer to your own site All pages must be dynamically generated Fails for bookmarks and links from other sites
9
Advantage
Works even if cookies are disabled or unsupported
Disadvantages
Lots of tedious processing All pages must be the result of form submissions
10
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Call removeAttribute discards a specific value. Call invalidate to discard an entire session.
13
If SomeClass is a mutable data structure (i.e., you didnt call new, but just modified the existing object, and you are using a normal (non distributed) application, then the call to setAttribute could be inside the if statement. But if it is an immutable data structure (i.e., you really created a new object, not modified the old one) or you are on a distributed/clustered app, you need to call setAttribute after modifying the value. Since it cant hurt to do this anyhow, it is a good practice to put the call to setAttribute after the part that modifies the session data.
Performance tip
Dont do synchronized(this)! Don t synchronized(this) !
Use the session or perhaps the value from the session as the label of the synchronized block
14
HttpSession Methods
getAttribute
Extracts a previously stored value from a session object. Returns null if no value is associated with given name.
setAttribute
Associates a value with a name. Monitor changes: values implement HttpSessionBindingListener. p p g
removeAttribute
Removes values associated with name.
getAttributeNames
Returns names of all attributes in the session.
getId tId
Returns the unique identifier.
16
getCreationTime
R Returns time at which session was first created i hi h i fi d
getLastAccessedTime
Returns time at which session was last sent from client
getMaxInactiveInterval, setMaxInactiveInterval
Gets or sets the amount of time session should go without access before being invalidated
invalidate
Invalidates current session
17
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
19
21
22
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
The warning is correct, since Java cannot verify that List correct contains only Strings. Still compiles and runs, but warning is annoying. You dont want to get in habit of ignoring warnings.
@SuppressWarnings("unchecked")
25
26
}}
27
28
Advanced Features
Customized Java EE Training: http://courses.coreservlets.com/
29
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
This is a tradeoff: session duplication can be expensive, but gives expensive you better load balancing
Solution
Explicitly send out the JSESSIONID cookie. p y
Do this at the beginning of the users actions Call setMaxAge first
Problem
Using a cookie with a large maxAge makes no sense unless the session timeout (inactiveInterval) is also large ( ) g An overly large session timeout can waste server memory
31
An On-Line Bookstore
Session tracking code stays the same as in simple examples i l l Shopping cart class is relatively complex
Id ifi items by a unique catalog ID Identifies i b i l Does not repeat items in the cart
Instead, each entry has a count associated with it If count reaches zero, item is deleted from cart
32
An On-Line Bookstore
33
An On-Line Bookstore
34
Wrap-up
Customized Java EE Training: http://courses.coreservlets.com/
35
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary
Sessions do not travel across network
Only unique identifier does
37
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
38
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.