One powerful feature of the Java Servlet API is its ability to process form data with relative ease. Instead of scrounging around for environment variables as with traditional CGI development, you can use servlets to access form and query-string data via normal Java method calls. Let’s take a look at how this works. You can download sample code for the article here.

Loading a servlet
When a servlet is loaded, the servlet engine makes a call to the servlet’s init method. This method, which is called only once as the servlet is loaded, is passed configuration data in the form of an instance of the ServletConfig class. The ServletConfig object contains information about the servlet engine and the environment where the servlet is running.

The ServletConfig object also exposes initialization data specific to the Web application in which the servlet operates. Using the getInitParameter(String  paramName) method, you can access this data, as Listing A demonstrates.

The eve of destruction
The servlet class exposes a method, destroy, that the servlet engine calls when the servlet unloads normally. This method is called when all of the servlet’s threads have unloaded or after a given timeout period. It’s used to save the state of the servlet to a persistent store or to clean up resources, as shown in Listing B.

More servlet articles

In “Get started with Java servlets,” we discussed how servlet classes offered by the Java platform facilitate building Web applications using similar object-oriented techniques employed in a fat client environment. To dive into Tomcat, check out “A beginner’s guide to Tomcat 4/JSP.”

One instance per customer, please
A servlet must be ready to handle multiple requests from multiple clients. Since any number of clients can simultaneously access a servlet, any method other than init must be implemented in a thread-safe manner.

Figure A shows the relationship between multiple client requests and a single servlet instance.

Figure A
Multiple client requests against a single servlet

How the cookies crumble
Since one servlet handles requests for multiple clients, a servlet needs a mechanism for determining which client originated a request. The most commonly used mechanism for client request identification is the use of HTTP cookies. HTTP cookies are arbitrary bits of information passed between a browser-based client and server using HTTP headers.

When a servlet receives a request from a client that has cookies enabled, the servlet can use the information inside the cookie to determine whom the client is and can therefore respond intelligently to the client. This may seem like a trivial feature, but it actually adds an entire dimension to the interaction between a client and a servlet-based Web application. Notably, this feature allows a Web application to save and restore the state of a client between requests.

The ability for a servlet to maintain client state allows a conversational session to transpire between a client and servlet-based Web application instead of restricting a client to a one-time request/response transaction. The Java servlet API provides a class named Cookie that represents cookie objects that can be retrieved from a request and stored in a response and that allows you to get and set information from a cookie. For example, the snippet in Listing C retrieves the cookies from a request, prints information about each cookie, and then creates and adds a new cookie to the response.

eCourt is now in session
With the ability to maintain state about a client between requests and responses, a servlet-oriented Web application developer can now effectively design Web applications that interact with clients over a period of time known as a session. A session can be used by a servlet developer to receive data from a client and pass data to a client depending on information received and sent in prior requests and/or responses. This facilitates effective Web applications such as shopping carts, online banking, and Web mail.

The Java servlet API provides a class named HttpSession in which a developer can store and retrieve arbitrary objects containing information regarding a client session. Objects are stored in and retrieved from the HttpSession class as name/value pairs. Listing D shows an example.

Using the Java servlet API, you can achieve powerful objectives that are usually difficult with traditional CGI programming, such as associating a request with a client to interact with a client in a meaningful, conversational manner. Our next article will expand this exploration of server-side development with a look at business logic components and middleware concepts.