Session Tracking Parts of this presentation was provided by www.coreservlets.com SSE.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
7 Copyright © 2005, Oracle. All rights reserved. Maintaining State in J2EE Applications.
Session Tracking Problem: Identifizierung und Speicherung persönlicher Daten Warenkorb Lösung: Session mit ID Anmeldung ID REQ + ID RES ID: JKLMGHNB45kdse43k.
1 Web Search Interfaces. 2 Web Search Interface Web search engines of course need a web-based interface. Search page must accept a query string and submit.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Authentication and Security Joshua Scotton.  Sessions  Login and Authentication.
Servlets and a little bit of Web Services Russell Beale.
Servlet Session Tracking. 2 Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information: Information.
Servlet Session Tracking II Session API All material and examples are from
All You Ever Wanted To Know About Servlets But Were Afraid to Ask.
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
Servlets, Sessions, and Cookies Lecture 8 cs193i – Internet Technologies Summer 2004 Kelly Shaw, Stanford University.
IT533 Lectures Session Management in ASP.NET. Session Tracking 2 Personalization Personalization makes it possible for e-businesses to communicate effectively.
Java Servlet Technology. Introduction Servlets are Java programs that run on a Web server, handle HTTP requests and build Web pages Servlet specification.
Servlets Pranav Maydeo. What is a Servlet ? Servlets are modules of Java code that run in a server application to answer client requests. Servlets are.
Chapter 5 Java Servlets. Objectives Explain the nature of a servlet and its operation Use the appropriate servlet methods in a web application Code the.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Session tracking There are a number of problems that arise from the fact that HTTP is a "stateless" protocol. In particular, when you are doing on- line.
16-Oct-15 JSP Implicit Objects. 2 JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer.
Session Tracking - 2 Lec 32. Last Lecture Review  Session Tracking – why?  Need to store state – typical solutions Cookies – already learned URL Rewriting.
1 Maryland ColdFusion User Group Session Management December 2001 Michael Schuler
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Web Application Development * These slides have been adapted and modified from CoreServlets course material (Marty Hall) and LUMS cs391 (Umair Javed).
Chapter 6 Server-side Programming: Java Servlets
Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Session Management.
STATE MANAGEMENT.  Web Applications are based on stateless HTTP protocol which does not retain any information about user requests  The concept of state.
® IBM Software Group © 2007 IBM Corporation Best Practices for Session Management
Web Database Programming Week 7 Session Management & Authentication.
Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their.
CSCI 6962: Server-side Design and Programming Java Server Faces Scoping and Session Handling.
All You Ever Wanted To Know About Servlets But Were Afraid to Ask.
JAVA Sessions 1. What is Session Tracking? There are a number of problems that arise from the fact that HTTP is a "stateless" protocol. In particular,
Saving State on the WWW. The Issue  Connections on the WWW are stateless  Every time a link is followed is like the first time to the server — it has.
CP476 Internet Computing CGI1 Cookie –Cookie is a mechanism for a web server recall info of accessing of a client browser –A cookie is an object sent by.
Session Tracking Lec 31. Revisiting Session Tracking HTTP is a stateless protocol  Every request is considered independent of every other request Many.
Cookies. Cookie A cookie is a piece of textual information Send by the Web server to the client browser Every time the browser visits the Web site again,
Li Tak Sing COMPS311F. A web page that counts the number of times that you have visited the page. You can try the page at:
Introduction to Servlets. Introduction Servlet is a language to develop the server side applications, and it is also server side component. It can develop.
Slides © Marty Hall, book © Sun Microsystems Press 1 Session Tracking Core Servlets & JSP book: More.
1 State and Session Management HTTP is a stateless protocol – it has no memory of prior connections and cannot distinguish one request from another. The.
©SoftMooreSlide 1 Session Tracking with Servlets.
CSC 2720 Building Web Applications Managing Users' States – Cookies, URL-Rewriting, Hidden Fields and Session Management APIs.
©SoftMooreSlide 1 Cookies. ©SoftMooreSlide 2 Cookies Basic idea –web application sends a simple name/value pair to the client –when the client connects.
1 Servlets – Part 2 Representation and Management of Data on the Web.
8-Mar-16 More About Servlets Session Tracking. Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information:
Distributed Web Systems Cookies and Session Tracking Lecturer Department University.
JSP Implicit Objects CS 422 Dick Steflik.
Sessions Many interactive Web sites spread user data entry out over several pages: Ex: add items to cart, enter shipping information, enter billing information.
Session Tracking in Servlets
Chapter 6 Server-side Programming: Java Servlets
Sessions.
SESSION TRACKING.
Servlets and Java Server Pages
Servlets CEN /28/2018 Copyright 2001 Ege Consulting, Inc.
CS320 Web and Internet Programming Cookies and Session Tracking
All You Ever Wanted To Know About Servlets
Handling State in Web Applications
CS3220 Web and Internet Programming Cookies and Session Tracking
Cookies Cookies are small bits of textual information that a Web server sends to a browser and that the browser returns unchanged when later visiting the.
Session Tracking Techniques
Sessions Many interactive Web sites spread user data entry out over several pages: Ex: add items to cart, enter shipping information, enter billing information.
Web Search Interfaces.
CS3220 Web and Internet Programming Cookies and Session Tracking
Pre-assessment Questions
Servlet Session Tracking: Session API
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Presentation transcript:

Session Tracking Parts of this presentation was provided by SSE

What is Session Tracking? l Capability of a server to maintain the current state of a single client’s sequential requests l HTTP is a “stateless” protocol, i.e., every transaction is autonomous l Need to keep track of “which client has performed what actions while at your site l Could use HTTP authentication – big hassle in high volume sites l Different ways to determine the actions that a particular client has taken

Session Tracking and E-Commerce l Advantages of session tracking: When clients at on-line store add item to their shopping cart, how does server know what’s already in cart? When clients decide to proceed to checkout, how can server determine which previously created cart is theirs?

Different ways of Session Tracking l Hidden Form Fields l Cookies l URL Rewriting l Built-in Session Tracking functionality of Servlet API

Rolling Your Own Session Tracking: Hidden Form Fields l Idea: Hidden Form Fields can be used to store information about the session l Advantage Easy to implement and supported by most browsers Works even if cookies are disabled or unsupported l Disadvantages Hidden fields must be created in a particular sequence Can’t use the back button without loosing information Lots of tedious processing All pages must be the result of form submissions

Example using Hidden Fields l Servlet that sends hidden fields to the browser Example: SendHiddenFields.java l User adds more data and submits the page – calls another servlet Example: MyHiddenFieldServlet.java l Combine data from the hidden fields and the new data l Go through a sequence of pages

Working with Cookies l Use persistent cookies to store client information l Cookie – created by server and stored by the browser during a visit l Subsequent visits can use the cookie to look up information related to that visit l Basically, associate a cookie to its corresponding visit data stored at the server side

Rolling Your Own Session Tracking: Cookies l Idea: associate cookie with data on server String sessionID = makeUniqueString(); Hashtable sessionInfo = new Hashtable(); Hashtable globalTable = findTableStoringSessions(); globalTable.put(sessionID, sessionInfo); Cookie sessionCookie = new Cookie("JSESSIONID", sessionID); sessionCookie.setPath("/"); response.addCookie(sessionCookie); l 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

Cookie Processing Example l Write a cookie l Get the cookie content l Based on the data contained in the cookie do additional processing l Example: CookieServlet.java

URL-Rewriting l If the browser does not support cookies or if cookies are disabled, then, URL-Rewriting provides an alternative for session tracking l In this approach, the requested URL is modified to include a session ID l The session ID value is used by the server to look up related data for that session

Rolling Your Own Session Tracking: URL-Rewriting l Idea Client appends some extra data on the end of each URL that identifies the session Server associates that identifier with data it has stored about that session E.g., l Advantage Works even if cookies are disabled or unsupported l Disadvantages Lots of tedious processing Must encode all URLs that refer to your own site Links from other sites and bookmarks can fail

Session Tracking with the Servlet API l Servlet API has its own built-in support for session tracking l The HttpSession object provides this functionality l Several methods within HttpSession setAttribute() Binds a name/value pair to store in the current session getAttribute() Used to get an object that is stored in that session getAttributeNames() Returns an array of the current bound names stored in the session object removeAttribute() Removes a binding from the current session

The Session Tracking API l Session objects live on the server l Automatically associated with client via cookies or URL- rewriting Use request.getSession(true) to get either existing or new session Behind the scenes, the system looks at cookie or URL extra info and sees if it matches the key to some previously stored session object. If so, it returns that object. If not, it creates a new one, assigns a cookie or URL info as its key, and returns that new session object. l Hashtable-like mechanism lets you store arbitrary objects inside session setAttribute (putValue in 2.1) stores values getAttribute (getValue in 2.1) retrieves values

Accessing Session Data HttpSession session = request.getSession(true); ShoppingCart cart = (ShoppingCart)session.getAttribute("shoppingCart"); if (cart == null) { // No cart already in session cart = new ShoppingCart(); session.setAttribute("shoppingCart", cart); } doSomethingWith(cart);

HttpSession Methods l getAttribute, getValue [2.1] Extracts a previously stored value from a session object. Returns null if no value is associated with given name. l setAttribute, putValue [2.1] Associates a value with a name. Monitor changes: values implement HttpSessionBindingListener. l removeAttribute, removeValue [2.1] Removes values associated with name. l getAttributeNames, getValueNames [2.1] Returns names of all attributes in the session. l getId Returns the unique identifier.

HttpSession Methods (Contd) l isNew Determines if session is new to client (not to page) l getCreationTime Returns time at which session was first created l 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 l invalidate Invalidates the session and unbinds all objects associated with it

Servlet Showing Per-Client Access Counts public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Session Tracking Example"; HttpSession session = request.getSession(true); String heading; Integer accessCount = (Integer)session.getAttribute("accessCount"); if (accessCount == null) { accessCount = new Integer(0); heading = "Welcome, Newcomer"; } else { heading = "Welcome Back"; accessCount = new Integer(accessCount.intValue() + 1); } session.setAttribute("accessCount", accessCount);

First Visit to ShowSession Servlet

Eleventh Visit to ShowSession Servlet

Session Tracking and Shopping Carts

Session Tracking and Shopping Carts (Continued)

Summary l Although it usually uses cookies behind the scenes, the session tracking API is higher-level and easier to use than the cookie API If server supports URL-rewriting, your code unchanged l Session information lives on server Cookie or extra URL info associates it with a user l Obtaining session request.getSession(true) l Associating values with keys session.setAttribute (or session.putValue) l Finding values associated with keys session.getAttribute (or session.getValue) Always check if this value is null