J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.

Slides:



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

7 Copyright © 2005, Oracle. All rights reserved. Maintaining State in J2EE Applications.
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.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Authentication and Security Joshua Scotton.  Sessions  Login and Authentication.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
Servlets and a little bit of Web Services Russell Beale.
2/16/2004 Dynamic Content February 16, /16/2004 Assignments Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day.
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.
A Servlet’s Job Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
J2EE training: 1 TODO Replace Netscape examples with Firefox. In later two sections also.
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.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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
Web Application Development * These slides have been adapted and modified from CoreServlets course material (Marty Hall) and LUMS cs391 (Umair Javed).
Java Servlets & Java Server Pages Lecture July 2013.
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.
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.
 Obtaining a RequestDispatcher Forwarding requests from servlets to dynamic resources  Forwarding requests from servlets to static resources  Using.
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:
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.
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.
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.
Java Servlets By: Tejashri Udavant..
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 Java Servlets
Handling State in Web Applications
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.
Pre-assessment Questions
Servlet Session Tracking: Session API
Presentation transcript:

J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting institutions –Slides not permitted for use in commercial training courses except when taught by coreservlets.com (see Slides can be modified however instructor desires Instructor can give PDF or hardcopy to students, but should protect the PowerPoint files –This slide is suppressed in Slide Show mode

JSP, Servlet, Struts, JSF & Java Training: J2EE Books from Sun Press: © 2005 Marty Hall 2 Session Tracking

J2EE training: 3 Agenda Implementing session tracking from scratch Using basic session tracking Understanding the session-tracking API Differentiating between server and browser sessions Encoding URLs Storing immutable objects vs. storing mutable objects Tracking user access counts Accumulating user purchases Implementing a shopping cart Building an online store

J2EE training: 4 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.

J2EE training: 5 Rolling Your Own Session Tracking: Cookies Idea: associate cookie with data on server String sessionID = makeUniqueString(); HashMap sessionInfo = new HashMap(); HashMap 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

J2EE training: 6 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 –Must encode all URLs that refer to your own site –All pages must be dynamically generated –Fails for bookmarks and links from other sites

J2EE training: 7 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

J2EE training: 8 Session Tracking in Java Session objects live on the server Sessions automatically associated with client via cookies or URL-rewriting –Use request.getSession to get 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 stores values –getAttribute retrieves values

J2EE training: 9 Session Tracking Basics Access the session object –Call request.getSession to get HttpSession object This is a hashtable associated with the user Look up information associated with a session. –Call getAttribute on the HttpSession object, cast the return value to the appropriate type, and check whether the result is null. Store information in a session. –Use setAttribute with a key and a value. Discard session data. –Call removeAttribute discards a specific value. –Call invalidate to discard an entire session.

J2EE training: 10 Session Tracking Basics: Sample Code HttpSession session = request.getSession(); SomeClass value = (SomeClass)session.getAttribute("someID"); if (value == null) { value = new SomeClass(...); session.setAttribute("someID", value); } doSomethingWith(value); –Do not need to call setAttribute again (after modifying value) if the modified value is the same object. But, if value is immutable, modified value will be a new object reference, and you must call setAttribute again.

J2EE training: 11 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 own site: –Pass URL through response.encodeRedirectURL

J2EE training: 12 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. removeAttribute –Removes values associated with name. getAttributeNames –Returns names of all attributes in the session. getId –Returns the unique identifier.

J2EE training: 13 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 current session

J2EE training: 14 A Servlet that Shows Per-Client Access Counts public class ShowSession extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); HttpSession session = request.getSession(); 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);

J2EE training: 15 A Servlet that Shows Per-Client Access Counts (Continued) PrintWriter out = response.getWriter(); … out.println (docType + " \n" + " " + title + " \n" + " \n" + " " + heading + " \n" + " Information on Your Session: \n" + " \n" + " Info Type Value\n" + … " Number of Previous Accesses\n" + " " + accessCount + "\n" + " \n" + " ");

J2EE training: 16 A Servlet that Shows Per-Client Access Counts: Result 1

J2EE training: 17 A Servlet that Shows Per-Client Access Counts: Result 2

J2EE training: 18 Accumulating a List of User Data public class ShowItems extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); ArrayList previousItems = (ArrayList)session.getAttribute("previousItems"); if (previousItems == null) { previousItems = new ArrayList(); session.setAttribute("previousItems", previousItems); }

J2EE training: 19 Accumulating a List of User Data (Continued) String newItem = request.getParameter("newItem"); PrintWriter out = response.getWriter(); … synchronized(previousItems) { if ((newItem != null) && (!newItem.trim().equals(""))) { previousItems.add(newItem); } if (previousItems.size() == 0) { out.println(" No items "); } else { out.println(" "); for(int i=0; i<previousItems.size(); i++) { out.println(" " + (String)previousItems.get(i)); } out.println(" "); } out.println(" "); }

J2EE training: 20 Accumulating a List of User Data: Front End

J2EE training: 21 Accumulating a List of User Data: Result

J2EE training: 22 An On-Line Bookstore Session tracking code stays the same as in simple examples Shopping cart class is relatively complex –Identifies items by a unique catalog ID –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 Pages built automatically from objects that have descriptions of books

J2EE training: 23 An On-Line Bookstore

J2EE training: 24 An On-Line Bookstore

J2EE training: 25 Distributed and Persistent Sessions Some servers support distributed Web applications –Load balancing used to send different requests to different machines –Session tracking still guaranteed to work Some servers suport persistent sessions –Session data written to disk and reloaded when server is restarted To support both, session data should implement the java.io.Serializable interface –There are no methods in this interface; it is just a flag.

J2EE training: 26 Summary Sessions do not travel across network –Only unique identifier does Get the session –request.getSession Extract data from session –session.getAttribute Do typecast and check for null Put data in session –session.setAttribute

JSP, Servlet, Struts, JSF & Java Training: J2EE Books from Sun Press: © 2005 Marty Hall 27 Questions?