7 Copyright © 2005, Oracle. All rights reserved. Maintaining State in J2EE Applications.

Slides:



Advertisements
Similar presentations
Servlets & JSPs - Sharad Ballepu.
Advertisements

Internet i jego zastosowania 1 J2EE Servlets. Internet i jego zastosowania 2 Agenda Overview Servlet Interface Servlet Context Request Response Sample.
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento.
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Communicating in J2EE.
19 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Developing Web Services.
21 Copyright © 2005, Oracle. All rights reserved. Oracle Application Server 10g Transaction Support.
12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.
1 Copyright © 2005, Oracle. All rights reserved. Introduction.
18 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Introduction to Web Services.
5 Copyright © 2005, Oracle. All rights reserved. Accessing the Database with Servlets.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
6 Copyright © 2005, Oracle. All rights reserved. Using Advanced Techniques in Servlets.
3 Copyright © 2005, Oracle. All rights reserved. Designing J2EE Applications.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
15 Copyright © 2005, Oracle. All rights reserved. Container-Managed Relationships (CMRs)
1 Copyright © 2005, Oracle. All rights reserved. Introducing the Java and Oracle Platforms.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
J2EE Overview.
6 Copyright © 2005, Oracle. All rights reserved. Building Applications with Oracle JDeveloper 10g.
17 Copyright © 2005, Oracle. All rights reserved. Deploying Applications by Using Java Web Start.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
JSP and Servelets.
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.
25 seconds left…...
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
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.
IT533 Lectures Session Management in ASP.NET. Session Tracking 2 Personalization Personalization makes it possible for e-businesses to communicate effectively.
Standalone Java Application vs. Java Web Application
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.
Session Tracking - 2 Lec 32. Last Lecture Review  Session Tracking – why?  Need to store state – typical solutions Cookies – already learned URL Rewriting.
Web Application Development * These slides have been adapted and modified from CoreServlets course material (Marty Hall) and LUMS cs391 (Umair Javed).
ASP.NET State Management. Slide 2 Lecture Overview Client state management options Cookies Server state management options Application state Session state.
© 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
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.
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:
Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
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.
Session Tracking Parts of this presentation was provided by SSE.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
8-Mar-16 More About Servlets Session Tracking. Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information:
Java Servlets References: Karen Anewalt, Mary Washington College.
Servlets.
Servlets.
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
Pre-assessment Questions
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.
CS3220 Web and Internet Programming Cookies and Session Tracking
Objectives In this lesson you will learn about: Need for servlets
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.
Knowledge Byte In this section, you will learn about:
CS3220 Web and Internet Programming Cookies and Session Tracking
Pre-assessment Questions
Servlet Session Tracking: Session API
Presentation transcript:

7 Copyright © 2005, Oracle. All rights reserved. Maintaining State in J2EE Applications

7-2 Copyright © 2005, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Maintain persistent activity from clients by using a browser Use the HttpSession object Describe state preservation

7-3 Copyright © 2005, Oracle. All rights reserved. First request Chris Second request Chris First request Michelle Servlet Overview

7-4 Copyright © 2005, Oracle. All rights reserved. Session Basics The HTTP protocol is stateless. The session mechanism guarantees that the object that serves the client knows which client has made a request. User requests from the same browser are considered to be from the same client.

7-5 Copyright © 2005, Oracle. All rights reserved. Session Basics Options for identifying the client: Cookies, URL rewriting, hidden fields, HttpSession Result: A unique identity assigned to every client Options for implementing sessions on the server: –Single-threaded model (not scalable) – HttpSession with a multithreaded server (Each thread uses the unique identity to process the request.)

7-6 Copyright © 2005, Oracle. All rights reserved. Threading Multithreaded modelSingle-threaded model Servlet instance 1 Servlet instance 2 Client 1 Client 2 Client 1 Client 2 Both clients using unique sessions, but sharing the same servlet instance Both clients using unique sessions and unique instances

7-7 Copyright © 2005, Oracle. All rights reserved. URL Rewriting URL rewriting: –Every URL that is accessed by the client is rewritten so that it has the session ID. –Use the encodeURL() method to re-create the path dynamically. URL rewriting is used when a client turns off cookie support in the browser.

7-8 Copyright © 2005, Oracle. All rights reserved. HttpSession The unique identity for the client is an HttpSession object. The object is created by using the getSession() method of the HttpRequest object. Any servlet that responds to a client request can create this object. An object can be potentially shared across several servlets. (Every servlet within an application can identify with this client.) HttpSession session = req.getSession(true);

7-9 Copyright © 2005, Oracle. All rights reserved. Session Objects With session objects, you can: –Put items into the object (values persist across multiple invocations from the same client) –Access items from the object –Obtain the session identity –Find out when the session was last accessed Items put in a session object can: –Implement the Serializable interface –Be relocated to a different server –Persist across servlet crashes

7-10 Copyright © 2005, Oracle. All rights reserved. public void doGet(…)… { response.setContentType("text/html"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(true); String sessionid = session.getId(); Integer sessionCount = (Integer)session.getAttribute("sessionCount"); if (sessionCount == null) { sessionCount = new Integer(0); } else { sessionCount = new Integer(sessionCount.intValue() + 1); } session.setAttribute("sessionCount", sessionCount); out.println(" Number of requests for the session with the id of " + " " + sessionid + " is: " + sessionCount); } Session-Based Page Counter

7-11 Copyright © 2005, Oracle. All rights reserved. Date dayAgo = new Date( System.currentTimeMillis() - 24*60*60*1000); Date hourAgo = new Date(…) // an hour ago Date created = new Date( session.getCreationTime()); Date accessed = new Date(…) if (created.before(dayAgo)|| accessed.before(hourAgo)) { session.invalidate(); session = … //create new } Session Life Cycle A session can expire automatically, or you can explicitly invalidate a session. The HttpSession object gets invalidated when a session expires.

7-12 Copyright © 2005, Oracle. All rights reserved. Session Tracking in OC4J J2EE server vendors handle session tracking in different ways. Oracle Application Server 10g Containers for J2EE (OC4J): –Uses cookies as the default method for session tracking (can be disabled by a user or within the application deployment descriptor) –Does not support auto-encoding, where session IDs are automatically encoded into the URL by the container (an expensive process) –Causes a session to expire in 20 minutes by default (modified in the deployment descriptor)

7-13 Copyright © 2005, Oracle. All rights reserved. Sessions and Events When a servlet stores an object in a session or removes an object from a session, the session checks whether that object implements the HttpSessionBindingListener interface. If it does, then the servlet notifies the object that it has been either: Bound to the session (by calling the objects valueBound() method, which is a good place for initializing client-specific resources) Or Unbound from a session (by calling the objects valueUnbound() method, which is a good place for releasing resources)

7-14 Copyright © 2005, Oracle. All rights reserved. Sessions and Events An object is bound to a session after the object is passed into the session.setAttribute() method. An object is unbound from a session: –After the object is removed by using the session.removeAttribute() method –When a session is invalidated

7-15 Copyright © 2005, Oracle. All rights reserved. Sessions and Events To use the event mechanism, you must perform the following steps: 1.Create a class that implements the HttpSessionBindingListener interface. 2.Instantiate the class. 3.Insert the instantiated object into the HttpSession object by using the setAttribute() method.

7-16 Copyright © 2005, Oracle. All rights reserved. public class EventObject implements HttpSessionBindingListener { public void valueBound( HttpSessionBindingEvent event) { // connect to the database using this client sess = event.getSession()//which session? // get values from this session object to // identify client information } public void valueUnbound( HttpSessionBindingEvent event) { // release resources } Sessions and Events Example:

7-17 Copyright © 2005, Oracle. All rights reserved. Creating Distributable Applications Applications are deployed as distributable by specifying the tag in the web.xml file. These applications should be developed to run in a distributed servlet container (such as OC4J) as follows: Because the ServletContext attributes exist locally in one Java virtual machine (JVM), the information that must be shared between servlets should be placed in a session, a database, or an EJB. HttpSession objects must implement the Serializable interface to be sent between JVMs.

7-18 Copyright © 2005, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Instantiate the HttpSession object Use the HttpSession object Implement the HttpSessionBindingListener interface

7-19 Copyright © 2005, Oracle. All rights reserved. Practice 7-1: Overview This practice covers the following topics: Creating an HttpSession object Tracking an order based on the HttpSession object

7-20 Copyright © 2005, Oracle. All rights reserved.

7-21 Copyright © 2005, Oracle. All rights reserved.

7-22 Copyright © 2005, Oracle. All rights reserved.