Home arrow Java arrow Page 7 - An Introduction to Java Servlets

An Introduction to Java Servlets

Java servlets are making headlines these days, claiming to solve many of the problems associated with CGI and proprietary server API's. In this article Nakul describes the overall servlet architecture and what you need to develop a web application using servlets. He uses several coding examples to show you how to use the servlet API and compares it with CGI and proprietary server API's.

Author Info:
By: Nakul Goyal
Rating: 4 stars4 stars4 stars4 stars4 stars / 53
March 11, 2002
  1. · An Introduction to Java Servlets
  2. · The dark ages
  3. · Servlets to the rescue!
  4. · The servlet runtime environment
  5. · Servlet interface and life cycle
  6. · Request and response objects
  7. · Persistent and Shared Data
  8. · ServletContext attributes
  9. · Request attributes and resources
  10. · Conclusion

print this article

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 Tracking

An 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)


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.
blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials