An Introduction to Java Servlets - Persistent and Shared Data
(Page 7 of 10 )
One of the more interesting features of the Servlet API is its support for persistent data. Since a servlet stays loaded between requests and all servlets are loaded in the same process, it's easy to remember information from one request to another and to let different servlets share data.
The Servlet API contains a number of mechanisms to support this directly. We'll look at some of them in detail below. Another powerful way to handle shared data is by using a singleton object.
Session TrackingAn HttpSession class was introduced in the 2.0 version of the Servlet API. Instances of this class can hold information for one user session between requests. You start a new session by requesting an HttpSession object from the HttpServletRequest in your doGet or doPost method:
HttpSession session = request.getSession(true);This method takes a boolean argument. If true is passed as the argument then a new session will be started if one doesn't exist. If false is passed as the argument then only an existing session is returned. The HttpSession object is unique for one user session only. The Servlet API supports two ways to associate multiple requests with a session: cookies and URL rewriting.
If cookies are used, a cookie with a unique session ID is sent to the client when the session is established. The client then includes the cookie in all subsequent requests so the servlet engine can figure out which session the request is associated with. URL rewriting is intended for clients that don't support cookies or when the user has disabled cookies.
With URL rewriting the session ID is encoded in the URLs that your servlet sends to the client. When the user clicks on an encoded URL, the session ID is sent to the server where it can be extracted and have the client request associated with the correct session. To use URL rewriting you must make sure all URLs that you send to the client are encoded with the encodeURL or encodeRedirectURL methods in HttpServletResponse.
An HttpSession can store any type of object. A typical example is a database connection allowing multiple requests to be part of the same database transaction, or information about purchased products in a shopping cart application so that the user can add items to their cart while browsing through the site. To save an object in an HttpSession you use the putValue method:
...
Connection con = driver.getConnection(databaseURL, user, password);
session.putValue("myappl.connection", con);
...In another servlet or the same servlet processing another request, you can get the object with the getValue method:
...
HttpSession session = request.getSession(true);
Connection con = (Connection)
session.getValue("myappl.connection");
if (con != null) {
// Continue the database transaction
...You can explicitly terminate (invalidate) a session with the invalidate method or let it be timed-out by the servlet engine automatically. The session times out if no request associated with the session is received within a specified interval. Most servlet engines allow you to specify the length of the interval through a configuration option. In the 2.1 version of the Servlet API there's also a setMaxInactiveInterval so you can adjust the interval to meet the needs of each individual application.
Next: ServletContext attributes >>
More Java Articles
More By Nakul Goyal