Servlet Session Tracking: Session API

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.
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.
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.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
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.
Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.
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.
Maintaining State MacDonald Ch. 9 MIS 324 MIS 324 Professor Sandvig Professor Sandvig.
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.
Web Application Development * These slides have been adapted and modified from CoreServlets course material (Marty Hall) and LUMS cs391 (Umair Javed).
Chapter 6 Server-side Programming: Java Servlets
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
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.
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,
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:
OOSSE Week 8 JSP models Format of lecture: Assignment context JSP models JSPs calling other JSPs i.e. breaking up work Parameter passing JSPs with Add.
Slides © Marty Hall, book © Sun Microsystems Press 1 Session Tracking Core Servlets & JSP book: More.
©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.
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,
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:
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
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.
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.
ITM 352 Cookies.
Chapter 6 Server-side Programming: Java Servlets
Pre-assessment Questions
Sessions.
SESSION TRACKING.
Servlet Session Tracking
Servlets and Java Server Pages
MIS Professor Sandvig MIS 324 Professor Sandvig
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
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.
CS3220 Web and Internet Programming Cookies and Session Tracking
Pre-assessment Questions
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Presentation transcript:

Servlet Session Tracking: Session API

Road Map Using the Java Session API Overview of what the Session API provides Extracting Data from the Session Extracting Session Information Adding Data to the Session

Overview of Session API Functionality

Overview of Session API Servlets include a built-in Session API. Enables you to very easily create applications that depend on individual user data. For example: Shopping Carts Personalization Services Maintaining state about the user’s preferences.

Using the Session API Steps to using the Java Session API Get the Session object from the HTTPRequest object. Extract Data from the user’s Session Object Extract information about the session object, e.g. when was the session created? Add data to the user’s Session Object.

Getting a Session Object To get the user’s session object, call the getSession() method of the HttpServletRequest class. Example: HttpSession session = request.getSession(); If user already has a session, the existing session is returned. If no session exists, a new one is created and returned. If you want to know if this is a new session, call the Session isNew() method.

Getting a Session Object If you want to disable creation of new sessions, pass false to the getSession() method. For example: HttpSession session = request.getSession(false); If no current session exists, you will now get back a null object.

Behind the Scenes When you call getSession() there is a lot going on behind the scenes. Each user is automatically assigned a unique session ID. How does this sessionID get to the user? Option 1: If the browser supports cookies, the servlet will automatically create a session cookie, and store the session ID within the cookie. (In Tomcat, the cookie is called: JSESSIONID) Option 2: If the browser does not support cookies, the servlet will try to extract the session ID from the URL.

Extracting Data from the Session

Extracting Data From Session The Session object works like a Hash Map that enables you to store any type of Java object. You can therefore store any number of keys and their associated values. To extract an existing object, use the getValue() or getAttribute() method. Note: As of Servlet 2.2, the getValue() method is now deprecated. Use getAttribute() instead. Note: acad doesn’t use Servlet 2.2; use getValue()

Extracting Data from Session The getAttribute () method will return an Object type, so you will need to perform a type cast. Example: Integer accessCount = (Integer)session.getAttribute("accessCount");

Extracting Data from Session Tip: If you want to get a list of all “keys” associated with a Session, use the getAttributeNames() method. This method returns an Enumeration of all Attribute names.

Additional Session Info. The Session API includes methods for determining Session specific information. public String getId(); Returns the unique session ID associated with this user, e.g. gj9xswvw9p public boolean isNew(); Indicates if the session was just created. public long getCreationTime(); Indicates when the session was first created. public long getLastAccessedTime(); Indicates when the session was last sent from the client.

Additional Methods public int getMaxInactiveInterval Determine the length of time (in seconds) that a session should go without access before being automatically invalidated. public void setMaxInactiveInterval (int seconds) Sets the length of time (in seconds) that a session should go without access before being automatically invalidated. A negative value specifies that the session should never time out.

Adding Data to the Session

Adding Data To Session To add data to a session, use the putAttribute() method, and specify the key name and value. Example: session.putAttribute("accessCount", accessCount); To remove a value, you can use the removeAttribute (String name) method.

Terminating Sessions public void invalidate() If the user does not return to a servlet for XX minutes*, the session is automatically invalidated and deleted. If you want to manually invalidate the session, you can call invalidate(). * For the exact number of minutes before automatic expiration, check the getMaxInactiveInterval() method.

Encoding URLs If a browser does not support cookies, you need some other way to maintain the user’s session ID. The Servlet API takes care of this for you by automatically appending the session ID to URLs if the browser does not support cookies. To automatically append the session ID, use the encodeURL () method.

Encoding URLs Example: String url = response.encodeURL (originalURL); Remember that if you do this, every single URL must include the sessionID. Since this is hard to ensure, lots of sites require cookies.

Example Session Code

Tracking Example: ShowSession Track the number of visits for each unique visitor. If this is a first time visit, the servlet creates an accessCount Integer variable and assigns it to the Session. If the user has visited before, the servlet extracts the accessCount variable, increments it, and assigns it to the Session. Servlet also displays basic information regarding the session, including: creation time and time of last access.

Tracking Example: Shopping Provides a simple shopping cart. Stores an ArrayList is the session; session attribute is called, “previousItems” Each time you add a new item, the item is added to the ArrayList.

Summary The Session API is a simple, powerful API that enables you to store session information about each user. The Session API hides all the ugly details from you, so you can focus on your specific application. Steps to using the Java Session API: Get the Session object from the HTTPRequest object. Extract Data from the user’s Session Object (getValue/getAttribute method) Add data to the user’s Session Object (putValue/setAttribute method)