Precise Java Google
Home  | J2SE  | J2EE  | Book  | Free Books  | Contact
Chapter 1  | Chapter 2  | Chapter 3

Chapter 2: Session tracking


The conversation between the browser and the web server in a web application is stateless since its protocol, HTTP, is stateless. For example, when a user is browsing through a catalog, the browser sends a separate request for each click on the catalog product and the web server sends a separate response with the requested product items. In this conversation, the web server is not aware of any previous request and response data, and does not keep any state.

Although most of the web applications contain this stateless behavior, some web applications need to implement stateful behavior. For instance, you may have to implement a shopping cart module as a part of your web application where the user adds items to the cart and server needs to maintain the items across user's multiple requests and show the user all the items in the cart after each request. In such instances, you have to establish a session to keep track of session data (the data that spans multiple requests). To keep track of session data; you can use cookies, URL rewriting, or HTML hidden fields to pass the session data back and forth between the browser and the server. In Java, you can also use stateful session beans or HttpSession to manage the session data completely on the server. Out of all these mechanisms, HttpSession is the most widely used. It stores the data internally and uses either cookies or URL rewriting to transfer the session id between browser and web server.

The HttpSession mechanism is a set of classes that you get as part of the Servlet API. It contains the HttpSession class and other important classes such as HttpSessionListener and HttpSessionEvent. When you use HttpSession as your session tracking mechanism, it can become one of the bottlenecks in your application if not used efficiently. In this chapter, you'll find the following recipes that show how to implement HttpSession tracking efficiently. And you'll also find interesting internal details about HttpSession when you go through these recipes.

  • Reducing memory usage by programmatically removing HttpSession objects

  • Configuring appropriate session timeout value to efficiently remove HttpSession objects from memory

  • Minimizing the size of HttpSession objects to increase efficiency of your web application

This chapter discusses the following recipes to solve these issues when you design a web application.



Feed back

We appreciate and welcome your comments on this section. Email (remove ZZZ which is placed to prevent spam). Please note that we may not be able to reply to all the emails due to huge number of emails that we receive but we appreciate your comments and feedback.


Copyright © 2001-2005, Ravi Kalidindi and Rohini Kalidindi. All rights reserved.