CSC 2720 Building Web Applications Managing Users' States – Cookies, URL-Rewriting, Hidden Fields and Session Management APIs.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

7 Copyright © 2005, Oracle. All rights reserved. Maintaining State in J2EE Applications.
Authentication and Security Joshua Scotton.  Sessions  Login and Authentication.
Servlets and a little bit of Web Services Russell Beale.
Servlet Session I: Cookie API Road Map  Creating Cookies  Cookie Attributes  Reading Cookies  Example 1: Basic Counter  Example 2: Tracking Multiple.
Handling Cookies Vijayan Sugumaran Decision and Information Sciences Oakland University Parts of this presentation was provided by
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
SE-2840 Dr. Mark L. Hornick1 Java Servlet-based web apps Servlet Architecture.
ASHIMA KALRA.  INTRODUCTION TO JSP INTRODUCTION TO JSP  IMPLICIT OBJECTS IMPLICIT OBJECTS  COOKIES COOKIES.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
Comp2513 Java Servlets and Sessions Daniel L. Silver, Ph.D.
Chapter 8 Handling Cookies. Cookie Cookies are small bits of textual information that a Web server sends to a browser and that the browser later returns.
JavaScript, Fourth Edition
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
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.
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).
Mark Dixon 1 03 – Passing Data between pages: Forms, Sessions, & Query Strings.
Chapter 6 Server-side Programming: Java Servlets
Cookies Web Browser and Server use HTTP protocol to communicate and HTTP is a stateless protocol. But for a commercial website it is required to maintain.
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.
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.
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:
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
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.
Cookies in Servlet A cookie is a small piece of information that is persisted between the multiple client requests. A cookie has a name, a single value,
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.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
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:
HTTP Transactions 1. 2 Client-Server Model 3 HTTP HyperText Transport Protocol Native protocol for WWW Sits on top of internet’s TCP/IP protocol HTTP.
Net-centric Computing Servlets & JSP. Lecture Outline  Tracking Sessions  Cookies  Examples  JSP  Differences between JSP and Servlets  JSP Constructs.
Distributed Web Systems Cookies and Session Tracking Lecturer Department University.
Upsorn Praphamontripong CS Design and Implementation of Software for the Web Fall 2016 State Handling on the Web.
JSP Implicit Objects CS 422 Dick Steflik.
Cookies Tutorial Cavisson Systems Inc..
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.
Java Servlets II: Session Tracking
SESSION TRACKING.
Servlet Session Tracking
Client / Session Identification Cookies
CS320 Web and Internet Programming Cookies and Session Tracking
Handling State in Java Servlets
Handling State in Web Applications
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
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.
Pre-assessment Questions
Servlet Session Tracking: Session API
Presentation transcript:

CSC 2720 Building Web Applications Managing Users' States – Cookies, URL-Rewriting, Hidden Fields and Session Management APIs

login.jsp bye.jspshow_acct.jsp Login Failed Logout Successful Login  A typical web application involves more than one servlet. How should these servlets share data with one another?  e.g., How does show_acct.jsp knows which user has successfully logged in and which user's account to show?

HTTP – A Stateless Protocol  Web servers don't retain information about users between requests.  Each HTTP request is like a phone call and the server hangs up the phone after serving each request.  Web application developers need alternative methods for maintaining users' states.  Cookies  URL-rewriting  Hidden fields in HTML forms  Platform/language specific APIs for managing sessions

Program Logic Architecture of Stand-Alone Program User Interface and Presentation State Information (Data) All the components of the program work in the same memory space and share data. There is only one copy of data per application.

Web App (Business Logic) Architecture of Web Application Client B (User Interface and Presentation) Clients and servers have their own memory space. Client A (User Interface and Presentation)

Web App (Business Logic) Architecture of Web Application Client B (User Interface and Presentation) State Information (Data) How and where should we keep the state information that uniquely define an application for client A and B respectively? Client A (User Interface and Presentation) ?

Web App (Business Logic) Architecture of Web Application Client B (User Interface and Presentation) Client A (User Interface and Presentation) State info Approach #1:  Clients kept the state info  Clients sent the state info back to the server-side program in every request  Server-side programs update the state info and sends them back to the clients to keep State info

Web App (Business Logic) Architecture of Web Application Client B (User Interface and Presentation) Client A (User Interface and Presentation) id2 Approach #2:  Server keeps the state info  Clients keep a unique ID (generated by the server)  Clients pass ID back to server on every request  Server-side programs look up state info stored in the local memory based on received ID. id1 State info id1 id2

Maintaining States and Session Tracking Approach #1 can be achieved through  Cookies  URL-rewriting  Hidden Fields in Form  Disadvantages: Require sending large amount of data which may include sensitive info between the client and the server on every request. Approach #2 can be achieved through  High-level APIs for maintaining sessions by the Servlet

Cookies  HTTP cookies are data sent by a server (on behalf of the server-side programs) to a browser to keep.  The browser keeps the data for a period of time.  The browser sends back the cookies to the server on every request (unless the cookie support is turned off).  The cookies are embedded in the HTTP header (and therefore not visible to the user).

Cookies  Shortcomings  User may turn off cookies support.  Data are kept with the browser  Users using the same browser share the cookies.  Limited number of cookies (20) per server/domain and limited size (4k bytes) per cookie  Client can temper with cookies  Modify cookie files, use JavaScript to create/modify cookies, etc.  Notes  Don't always rely on cookies as the client may have turned off cookies support.  Don't store sensitive info in cookies

URL-Rewriting  Data are appended to the URL  e.g.:  Data are kept along with the "page"  Each time, a server-side program needs to  Retrieve all state info from the URL  Update the state info  Write the state info back to the URL in all links that appears in the page (including the URL in the form "action" attribute)  Shortcoming:  Limited number of characters in an URL  Not suitable for sensitive info

Hidden Fields in HTML Form  Data are encoded as hidden fields in HTML form as:  Shortcoming: Always need a form. <% String attemptParam = request.getParameter("attempt"); int attempt = 0; if (attemptParam != null) attempt = Integer.parseInt(attemptParam) + 1; // Assume no error in conversion %> Login ID Password " />

Session (Supported by the Servlet Container)  A session is a period of time in which all activities happened within the period by the same client is considered "related" (typically belong to the same application.)  Session Tracking – keeping track of users as they traverse from one web page or servlet to another within a website (or using a web application).  Clients keep a unique "session id" generated by the server.  The "session id" is sent back and forth between the client and the server through methods like cookies or URL- rewriting.

Creating and Obtaining Session object // e.g., in processRequest/doGet/doPost methods HttpSession session = request.getSession();  HttpServletRequest  public HttpSession getSession();  Returns the current session object. If there is no current session object, create a new one and return it.  In JSP, the "session" object is implicitly made available.  We can store data to be shared among servlets/JSP in the session object.

Session object: Storing and Accessing Data // We can store objects in the session object session.setAttribute("username", name); session.setAttribute("login-attempt", new Integer(0)); String name = (String)session.getAttribute("username"); session.removeAttribute("username");  HttpSession  public void setAttribute(String name, Object value);  public Object getAttribute(String name);  public void removeAttribute(String value);  Works like a hash table except that the key is a string.  You do not have to know what the generated session ID is.

More HttpSession Methods  public Enumeration getAttributeNames();  Returns names of all attributes set in the session object  public String getID();  Returns the unique session ID generated by the servlet container.  public boolean isNew();  Returns true if the clients does not yet know about the session.  public void invalidate();  Makes the session object invalid. Future use to this session object will result in IllegalStateException thrown.

More HttpSession Methods  public void setMaxInactiveInterval(int seconds)  Sets how long the session object can stay inactive before becoming invalid.  Any user request to the resources that belong to the same web application would activate the session object.  By default, the maximum inactive interval is set to 1800 seconds (30 min)  If seconds is -1, then the session will only terminate when the client terminates (when the user closes the browser).  public int getMaxInactiveInterval();  Returns the max inactive interval in seconds.

Using Session When Cookies is Disabled  Use URL-rewriting method to pass session ID to client  Use when cookies is disabled.  HttpResponse  public String encodeURL(String url);  Call this method to create a new URL string with session ID appended to "url".  Only append when necessary (e.g., when the client's browser does not support cookies or turns off cookies support).

Combined Use  All of Cookies, URL-rewriting, Hidden Fields, and Session objects can be simultaneously used in a web application.  Cookies: Can persist data for long period but is not suitable for keeping sensitive data or large amount of data.  URL-rewriting: Keep data along with page  Hidden Fields: Keep data along with page (can keep more data but requires HTML form)  Session Objects: Keep "short-live" data shared among the servlets within a web application for a particular client.

Summary  Session Management  Cookies  URL-Rewriting  Hidden Fields in HTML Form  High level APIs in Java and HttpSession Objects.  Reference 

Sending Cookies to Browser  Standard approach: Cookie c = new Cookie("name", "value"); c.setMaxAge(...); // Means cookie persists on disk // Set other attributes. response.addCookie(c);  Simplified approach:  Use LongLivedCookie class: public class LongLivedCookie extends Cookie { public static final int SECONDS_PER_YEAR = 60*60*24*365; public LongLivedCookie(String name, String value) { super(name, value); setMaxAge(SECONDS_PER_YEAR); }

Reading Cookies from Browser  Standard approach: Cookie[] cookies = request.getCookies(); if (cookies != null) { for(int i=0; i<cookies.length; i++) { Cookie c = cookies[i]; if (c.getName().equals("someName")) { doSomethingWith(c); break; }

public static String getCookieValue( Cookie[] cookies, String cookieName, String defaultVal) { if (cookies != null) { for(int i=0; i<cookies.length; i++) { Cookie cookie = cookies[i]; if (cookieName.equals(cookie.getName())) return(cookie.getValue()); } return(defaultVal); } Reading Cookies from Browser

Methods in the javax.servlet.http.Cookie  getDomain/setDomain  This property indicates the domain within which this cookie should be presented. (Default is the domain of the current server)  getMaxAge/setMaxAge  Gets/sets the cookie expiration time in seconds  A negative value means that the cookie is not stored persistently and will be deleted when the browser exits. (Default)  A zero value causes the cookie to be deleted.  getName  Gets the cookie name.  Cookie name can only be set through the constructor.

Methods in the javax.servlet.http.Cookie  getPath/setPath  Gets/sets the path to which the cookie applies.  The cookie is visible to all the pages in the directory you specify, and all the pages in that directory's subdirectories. A cookie's path must include the servlet that set the cookie  getSecure/setSecure  Gets/sets a flag that indicates whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL.  getValue/setValue  Gets/sets value associated with the cookie.  If you use a binary value, you may want to use BASE64 encoding.  With Version 0 cookies, values should not contain white space, brackets, parentheses, equals signs, commas, double quotes, slashes, question marks, at signs, colons, and semicolons. Empty values may not behave the same way on all browsers.