Web Application Development * These slides have been adapted and modified from CoreServlets course material (Marty Hall) and LUMS cs391 (Umair Javed).

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.
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.
XP Tutorial 9 New Perspectives on JavaScript, Comprehensive1 Working with Cookies Managing Data in a Web Site Using JavaScript Cookies.
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.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
Comp2513 Java Servlets and Sessions Daniel L. Silver, Ph.D.
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.
JavaScript, Fourth Edition
Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.
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.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Session Tracking - 2 Lec 32. Last Lecture Review  Session Tracking – why?  Need to store state – typical solutions Cookies – already learned URL Rewriting.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Chapter 6 Server-side Programming: Java Servlets
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
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
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:
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.
Servlets 4 Lec 30 Web Design and Development. Looking Back… Response Redirection  Sending a standard redirect  Sending a redirect to an error page Request.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
Session Tracking Parts of this presentation was provided by SSE.
8-Mar-16 More About Servlets Session Tracking. Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information:
Persistence Maintaining state: Queries. State is the Problem What is state? facebook status logins (which user are you?) conversations talking about what?
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
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.
Chapter 6 Server-side Programming: Java Servlets
Sessions.
SESSION TRACKING.
Servlet Session Tracking
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
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.
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:

Web Application Development * These slides have been adapted and modified from CoreServlets course material (Marty Hall) and LUMS cs391 (Umair Javed).

 HTTP is a stateless protocol  Every request is considered independent of every other request  Many web applications need to maintain a conversational state with the client  A shopping cart is a classic example

 Example Conversations  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?

 Server Side?  Makes Server Really Complicated  State per client!  Client Side?

 Server puts little notes on the client side  When client submits the next form, it also (unknowingly) submits these little notes  Server reads the notes, remembers who the client is

Credit: Programming the World Wide Web Book by Sebesta

 Cookies  Advantages ▪ Cookies do not require any server resources since they are stored on the client. ▪ Cookies are easy to implement. ▪ You can configure cookies to expire when the browser session ends (session cookies) or they can exist for a specified length of time on the client computer (persistent cookies).  Disadvantages ▪ Users can delete cookies. ▪ Users browser can refuse cookies, so your code has to anticipate that possibility.

 URL Rewriting  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

 For example, the following URLs have been rewritten to pass the session id 123  Original  Extra path information  Added parameter  Custom change

 Hidden Fields  Advantage ▪ Works even if cookies are disabled or unsupported  Disadvantages ▪ Lots of tedious processing ▪ All pages must be the result of form submissions

 Session objects live on the server  Automatically associated with client via cookies or URL-rewriting  Checks for a cookie or URL extra info

1.To get the user’s session object  Call getSession( ) method of HTTPServletRequest class  pass false to the getSession() method HttpSession ses = request.getSession(false);  If no current session exists: ▪ You will get a null object

1.To get the user’s session object (cont.)  If true is passed to the getSession() method then  If user already has a session ▪ the existing session is returned  For example: HttpSession ses = request.getSession(true);  If no session exists ▪ a new one is created and returned

2. Storing information in a session  Session objects works like a HashMap ▪ HashMap is able to store any type of java object  You can therefore store any number of keys and their values  For example ses.setAttribute(“id”, “123”); keyValue

3. Looking up information associated with a session String sID = (String)ses.getAttribute(“id”); returns an Object type, so you will need to perform a type cast

4. Terminating session  Automatic ▪ After the amount of time session gets terminated automatically( getMaxInactiveInterval( ) )  Manual ses.invalidate();

 HttpServletResponse provides two methods to perform encoding 1. String encodeURL(String URL) 2. String encodeRedirectURL(String URL)  If Cookies disabled  Both methods encodes (rewrites) the specified URL to include the session ID and returns the new URL  If Cookies enabled  Returns the URL unchanged

1. String encodeURL(String URL)  For example String URL = “/servlet/sessiontracker”; String eURL = response.encodeURL(URL); out. println("... ");

2. String encodeRedirectURL(String URL)  For example String URL = “/servlet/sessiontracker”; String eURL = response.encodeRedirectURL(URL); response.sendRedirect(eURL);

Amazon Servlet Container Session ID = 123XYZ Shopping Cart sc [item 1=324] Request Credit: cs193i at Standford

Amazon Servlet Container Session ID = 123XYZ Shopping Cart sc [item 1=324] Response: Set-Cookie: sid=123XYZ Credit: cs193i at Standford

Amazon Servlet Container Session ID = 123XYZ Shopping Cart sc [item 1=324] Request: Set-Cookie: sid=123XYZ Credit: cs193i at Standford

Amazon Servlet Container Session ID = 123XYZ Shopping Cart sc [item 1=324 item 2=115] Request: Set-Cookie: sid=123XYZ Credit: cs193i at Standford

 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.

 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

 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