Slides © Marty Hall, book © Sun Microsystems Press 1 Session Tracking Core Servlets & JSP book: More.

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.
Integrating Servlets and JavaServer Pages Vijayan Sugumaran School of Business Administration Oakland University Parts of this presentation provided by.
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 10 Servlets and Java Server Pages. A servlet is a Java class designed to be run in the context of a special servlet container An instance of the.
Servlets, Sessions, and Cookies Lecture 8 cs193i – Internet Technologies Summer 2004 Kelly Shaw, Stanford University.
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.
Slides © Marty Hall, book © Sun Microsystems Press 1 Including Files & Applets in JSP Documents Core Servlets & JSP book:
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Slides © Marty Hall, book © Sun Microsystems Press 1 Generating the HTTP Response Core Servlets & JSP book:
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
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 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.
® IBM Software Group © 2007 IBM Corporation Best Practices for Session Management
Slides © Marty Hall, book © Sun Microsystems Press 1 Handling the Client Request: Form Data Core Servlets & JSP book:
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.
S ERVLETS Hits Counter 21-Nov-15. S ERVLETS - H ITS C OUNTER Many times you would be interested in knowing total number of hits on a particular page of.
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,
Session Tracking Lec 31. Revisiting Session Tracking HTTP is a stateless protocol  Every request is considered independent of every other request Many.
Slides © Marty Hall, book © Sun Microsystems Press 1 Handling Cookies Core Servlets & JSP book: More.
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:
Slides © Marty Hall, book © Sun Microsystems Press 1 Using JavaBeans with JSP Core Servlets & JSP book:
ORIGINAL BY WENDY LIU CURRENT API: Java Servlet and JSP 1.
Introduction to Servlets. Introduction Servlet is a language to develop the server side applications, and it is also server side component. It can develop.
Vakgroep Informatietechnologie – Onderzoeksgroep (naam) Web Centric Design of Distributed Software.
©SoftMooreSlide 1 Session Tracking with Servlets.
CSC 2720 Building Web Applications Managing Users' States – Cookies, URL-Rewriting, Hidden Fields and Session Management APIs.
Session Tracking Parts of this presentation was provided by SSE.
©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.
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.
Web Search Interfaces by Ray Mooney
CS3220 Web and Internet Programming Cookies and Session Tracking
Pre-assessment Questions
Servlet Session Tracking: Session API
Presentation transcript:

Slides © Marty Hall, book © Sun Microsystems Press 1 Session Tracking Core Servlets & JSP book: More Servlets & JSP book: Servlet and JSP Training Courses: courses.coreservlets.com

Session Tracking2 Agenda The purpose of session tracking Rolling your own session tracking The session tracking API Per-client access counts Shopping carts and online stores

Session Tracking3 Session Tracking and E-Commerce Why 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? Dilbert used with permission of United Syndicates Inc.

Session Tracking4 Rolling Your Own Session Tracking: Cookies 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); 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

Session Tracking5 Rolling Your Own Session Tracking: URL-Rewriting 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., Advantage –Works even if cookies are disabled or unsupported Disadvantages –Lots of tedious processing –Must encode all URLs that refer to your own site –Links from other sites and bookmarks can fail

Session Tracking6 Rolling Your Own Session Tracking: Hidden Form Fields Idea: Advantage –Works even if cookies are disabled or unsupported Disadvantages –Lots of tedious processing –All pages must be the result of form submissions

Session Tracking7 The Session Tracking API Session objects live on the server 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. Hashtable-like mechanism lets you store arbitrary objects inside session –setAttribute (putValue in 2.1) stores values –getAttribute (getValue in 2.1) retrieves values

Session Tracking8 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);

Session Tracking9 HttpSession Methods getAttribute (getValue in old servlet spec 2.1) –Extracts a previously stored value from a session object. Returns null if no value is associated with given name. setAttribute (putValue in ver. 2.1) –Associates a value with a name. Monitor changes: values implement HttpSessionBindingListener. removeAttribute (removeValue in ver. 2.1) –Removes values associated with name. getAttributeNames (getValueNames in 2.1) –Returns names of all attributes in the session. getId –Returns the unique identifier.

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

Session Tracking11 A 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);

Session Tracking12 First Visit to ShowSession Servlet

Session Tracking13 Eleventh Visit to ShowSession Servlet

Session Tracking14 Session Tracking and Shopping Carts

Session Tracking15 Session Tracking and Shopping Carts (Continued)

Session Tracking16 What Changes if Server Uses URL Rewriting? Session tracking code: –No change Code that generates hypertext links back to same site: –Pass URL through response.encodeURL. If server is using cookies, this returns URL unchanged If server is using URL rewriting, this appends the session info to the URL E.g.: String url = "order-page.html"; url = response.encodeURL(url); Code that does sendRedirect to same site: –Pass URL through response.encodeRedirectURL

Session Tracking17 Summary 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 Session information lives on server –Cookie or extra URL info associates it with a user Obtaining session –request.getSession(true) Associating values with keys –session.setAttribute (or session.putValue) Finding values associated with keys –session.getAttribute (or session.getValue) Always check if this value is null

Slides © Marty Hall, book © Sun Microsystems Press 18 Questions? Core Servlets & JSP book: More Servlets & JSP book: Servlet and JSP Training Courses: courses.coreservlets.com