Distributed Web Systems Cookies and Session Tracking Lecturer Department University.

Slides:



Advertisements
Similar presentations
CookiesPHPMay-2007 : [‹#›] Maintaining State in PHP Part I - Cookies.
Advertisements

UFCE8V-20-3 Information Systems Development 3 (SHAPE HK)
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
Cookie in a servlet. Cookies are small bits of textual information that a Web server sends to a browser and that the browser returns unchanged when visiting.
DT228/3 Web Development multi page applications/ sharing data.
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.
1 The World Wide Web. 2  Web Fundamentals  Pages are defined by the Hypertext Markup Language (HTML) and contain text, graphics, audio, video and software.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
ASHIMA KALRA.  INTRODUCTION TO JSP INTRODUCTION TO JSP  IMPLICIT OBJECTS IMPLICIT OBJECTS  COOKIES COOKIES.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 15: PHP Introduction.
HTTP: cookies and advertising Concepts to cover:  web page content (including ads) from multiple site: composition at client  cookies  third-party cookies:
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
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.
IT533 Lectures Session Management in ASP.NET. Session Tracking 2 Personalization Personalization makes it possible for e-businesses to communicate effectively.
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.
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
Web Programming Language Week 7 Dr. Ken Cosh Security, Sessions & 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.
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.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
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).
Mark Dixon 1 03 – Passing Data between pages: Forms, Sessions, & Query Strings.
Chapter 6 Server-side Programming: Java Servlets
Cookies & Session Web Technology
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
Christopher M. Pascucci Basic Structural Concepts of.NET Managing State & Scope.
© 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.
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.
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.
SE-2840 Dr. Mark L. Hornick1 Servlet Threads and Sessions.
Slides © Marty Hall, book © Sun Microsystems Press 1 Session Tracking Core Servlets & JSP book: More.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
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.
MIT AITI 2004 JSP – Lecture 4 Sessions and Cookies.
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.
©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:
1 Chapter 22 World Wide Web (HTTP) Chapter 22 World Wide Web (HTTP) Mi-Jung Choi Dept. of Computer Science and Engineering
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.
Servlet Sessions and Cookies
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.
Servlet Session Tracking
Client / Session Identification Cookies
CS320 Web and Internet Programming Cookies and Session Tracking
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.
CSc 337 Lecture 27: Cookies.
CS3220 Web and Internet Programming Cookies and Session Tracking
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
CSc 337 Lecture 25: Cookies.
Presentation transcript:

Distributed Web Systems Cookies and Session Tracking Lecturer Department University

Outline Cookies Session tracking Data sharing between servlets

Need for cookies Consider an on-line shopping scenario: But HTTP is a connection-less protocol ! –Once a page is downloaded, the connection between the browser and the web server is closed –Any subsequent request will look like new to the web server –How can we make the web server recognise requests from the same user ? User’s PCWeb server choose 1 st product choose 2 nd product checkout

Cookies let you do that! Small bits of textual information: –Web server can send them to browser to store on the client side –The browser later returns this information unchanged when visiting the same Web site User’s PC Keep this for me please! cookie websitecookie blah User’s PC You left this here last time cookie

Example uses Identifying the user for e-commerce transactions Remembering user name, password (bad idea, actually) Remembering user preferences to customize websites Remembering what user did last time on this website (e.g. for targeted advertising etc)

Problems with cookies Privacy! banner ads search engine on-line shop Leave a cookie with information about the use of the search engine Read the cookie – learn about user’s browsing patterns for another site (search engine)

Sending cookies in servlets Create a Cookie object: –Cookie mycookie = new Cookie(“name”, “value”); Set maximum age of the cookie: –We need to tell the client’s browser how long it should keep the cookie for –cookie.setMaxAge(60*60*24*7); Place cookie into HTTP response headers: –response.addCookie(cookie); –This adds the “Cookie” header to the response time in seconds (a week here)

Reading cookies from client Call request.getCookies(): –Returns an array of Cookie objects – all cookies for this website stored on the client Loop down the array, calling getName() on each Cookie until you find the cookie of interest (i.e. the cookie set by your servlet): Cookie[] cookies = request.getCookies(); if (cookies != null) { for (int i = 0; i<cookies.length; i++) if (cookies[i].getName().equals(“MyCookie”) { // do something with the cookie }

Persistent vs Session Cookies Persistent cookie: –Stored on disk – available even after the browser application is closed and restarted Session cookie: –Stored in the browser’s memory. Once you close the browser application, the cookie is lost. Unless you use setMaxAge(), your cookies will be created as session cookies. Apart from name and age, cookies have some other interesting attributes (please see Core Servlets & JSP for details).

Session tracking Recall the problem we started with: –Need to realise on the web server that a set of separate HTTP requests actually belongs to the same interaction session between user and application. In general, three possible approaches: –Cookies –URL rewriting –Hidden form fields User’s PC Web server choose 1 st product choose 2 nd product checkout

Session tracking (contd.) URL rewriting: –Append some extra data to the end of each URL (e.g. –Problem 1: since the session ID changes, need to generate ALL pages dynamically (inconvenient) –Problem 2: if user leaves website and then comes back via a bookmark, session information can be lost Hidden form fields: –Have hidden entries in forms: –Problem: every page has to be generated dynamically but also ONLY by a form submission

What to do with session ID? Ok, you got your session ID (one way or another), what to do with it on the server? –Need to have some sort of a hash table that links the ID with the associated session data –Need to determine idle sessions and delete expired IDs –Need to generate unique IDs for new sessions User’s PC ID Data processing

Session tracking in servlets Fortunately, servlets and servlet containers already implement this functionality – providing you with the HttpSession object User’s PC HttpSession … s = request.getSession(); // read-write session data … Servlet container Servlet Get the session associated with current request IDData session ID

Session tracking in servlets (cont.) What if the user’s request does not have session ID (e.g. it’s the first user’s visit)? User’s PC HttpSession … s = request.getSession(); // write session data … Servlet container Servlet Get the session associated with current request IDData NO session ID create new Send back cookie with ID Because this may need to send cookies back, it must be called before sending any document content !!!

Session tracking in servlets (contd.) What if I do not want to create a new session when none exists? –E.g. my page needs data from a previous page and will not work correctly otherwise? Use getSession(false) –Returns the current session if one exists –or null otherwise

Reading/writing session data session.getAttribute(“name”) –Get information (object) associated with attribute session.getAttributeNames() –Get names of all attributes in the session session.setAttribute(“name”, Object value) –Store information (object) in the session (replaces the previous value for this attribute if any) session.removeAttribute(“name”) –Remove attribute from the session session.invalidate() –Invalidate all session data (=forget all objects associated with the session and remove the session itself) Use with caution!

Browser vs Server sessions By default, session-tracking is based on cookies that are stored in the browser’s memory (session cookies) –When user restarts the browser – the session is lost –But what about the session data on the server? –Use expire upon inactivity mechanism! User’s PC Session IDSession data

Servlet context What if we want to store data that are not specific to a particular user and that we want to be accessible by all servlets in our application? ServletContext object is the place! –getServletContext() -- returns the ServletContext object –Data access is the same as for the Session object –Data are stored for as long as your application is running (restarting application or the servlet container deletes the data) ServletContext … ServletContext c = getServletContext(); // read-write application data … ServletServlet container

Data sharing summary Storing MethodData shared betweenData lost when Servlet instance variables All requests to this particular servlet from any user Servlet is destroyed HttpSessionAll requests from this particular user to any servlet in the application User’s browser is restarted or server session expires/is invalidated ServletContextAll requests from any user to any servlet in this application The application is restarted

Summary Session tracking is used to provide a stateful interaction between a user and a web server via the stateless HTTP protocol Data stored on the server, session ID on the client –Cookies, URL rewriting, Hidden form fields Servlet context can be used for sharing data between servlets Questions