Servlet 2.3: New features exposed

A full update on the latest Servlet API spec

Summary
In October 2000, Sun released the "Proposed Final Draft" specification for Servlet API 2.3. This article explains the differences between Servlet API 2.2 and 2.3, discusses the reasons for the changes, and shows you how to write servlets (and now filters!) using 2.3. (4,000 words)
By Jason Hunter

On Oct. 20, 2000, Sun Microsystems published the "Proposed Final Draft" of the Servlet API 2.3 specification. (See Resources for a link to the formal specification.) Although the spec was published by Sun, Servlet API 2.3 was actually developed by the many individuals and companies working on the JSR-053 expert group, in accordance with the Java Community Process (JCP) 2.0. Danny Coward of Sun Microsystems led the servlet expert group.

The specification is not quite finished; the Proposed Final Draft is one step away from a formal Final Release, and technical details are still subject to change. However, those changes should not be significant -- in fact, server vendors have already begun to implement the new features. That means now is a good time to start learning about what's coming in Servlet API 2.3.

In this article, I will describe in detail everything that changed between API 2.2 and API 2.3. I will also explain the reasons for the changes and demonstrate how to write servlets using the new features. To keep the article focused, I will assume you're familiar with the classes and methods of previous versions of the Servlet API. If you're not, you can peruse the Resources section for links to sites (and my new book!) that will help get you up to speed.

Servlet API 2.3 actually leaves the core of servlets relatively untouched, which indicates that servlets have reached a high level of maturity. Most of the action has involved adding new features outside the core. Among the changes:

Other clarifications have been made, but they mostly concern server vendors, not general servlet programmers (except for the fact that programmers will see improved portability), so I'll omit those details.

Before I begin my examination, let me point out that version 2.3 has been released as a draft specification only. Most of the features discussed here won't yet work with all servers. If you want to test those features, I recommend downloading the official reference implementation server, Apache Tomcat 4.0. It's open source, and you can download the server for free. Tomcat 4.0 is currently in beta release; its support for API 2.3 is getting better, but is still incomplete. Read the NEW_SPECS.txt file that comes with Tomcat 4.0 to learn its level of support for all new specification features. (See Resources for more information on Tomcat.)

Servlets in J2SE and J2EE
One of the first things you should note about Servlet API 2.3 is that servlets now depend on the Java 2 Platform, Standard Edition 1.2 (also known as J2SE 1.2 or JDK 1.2). This small, but important, change means you can now use J2SE 1.2 features in your servlets and be guaranteed that the servlets will work across all servlet containers. Previously, you could use J2SE 1.2 features, but servers were not required to support them.

The Servlet API 2.3 is slated to become a core part of Java 2 Platform, Enterprise Edition 1.3 (J2EE 1.3). The previous version, Servlet API 2.2, was part of J2EE 1.2. The only noticeable difference is the addition of a few relatively obscure J2EE-related deployment descriptor tags in the web.xml DTD: <resource-env-ref> to support "administered objects," such as those required by the Java Messaging System (JMS); <res-ref-sharing-scope> to allow either shared or exclusive access to a resource reference; and <run-as> to specify the security identity of a caller to an EJB. Most servlet authors need not concern themselves with those J2EE tags; you can get a full description from the J2EE 1.3 specification.

Filters
The most significant part of API 2.3 is the addition of filters -- objects that can transform a request or modify a response. Filters are not servlets; they do not actually create a response. They are preprocessors of the request before it reaches a servlet, and/or postprocessors of the response leaving a servlet. In a sense, filters are a mature version of the old "servlet chaining" concept. A filter can:

You can configure a filter to act on a servlet or group of servlets; that servlet or group can be filtered by zero or more filters. Practical filter ideas include authentication filters, logging and auditing filters, image conversion filters, data compression filters, encryption filters, tokenizing filters, filters that trigger resource access events, XSLT filters that transform XML content, or MIME-type chain filters (just like servlet chaining).

A filter implements

javax.servlet.Filter
and defines its three methods:

The server calls

setFilterConfig()
once to prepare the filter for service, then calls
doFilter()
any number of times for various requests. The
FilterConfig
interface has methods to retrieve the filter's name, its init parameters, and the active servlet context. The server passes null to
setFilterConfig()
to indicate that the filter is being taken out of service.

Each filter receives in its

doFilter()
method the current request and response, as well as a
FilterChain
containing the filters that still must be processed. In the
doFilter()
method, a filter may do what it wants with the request and response. (It could gather data by calling their methods, or wrap the objects to give them new behavior, as discussed below.) The filter then calls
chain.doFilter()
to transfer control to the next filter. When that call returns, a filter can, at the end of its own
doFilter()
method, perform additional work on the response; for instance, it can log information about the response. If the filter wants to halt the request processing and gain full control of the response, it can intentionally not call the next filter.

A filter may wrap the request and/or response objects to provide custom behavior, changing certain method call implementation to influence later request handling actions. API 2.3 provides new

HttpServletRequestWrapper
and
HttpServletResponseWrapper
classes to help with this; they provide default implementations of all request and response methods, and delegate the calls to the original request or response by default. That means changing one method's behavior requires just extending the wrapper and reimplementing one method. Wrappers give filters great control over the request-handling and response-generating process. The code for a simple logging filter that records the duration of all requests is shown below:


public class LogFilter implements Filter {
  FilterConfig config;

  public void setFilterConfig(FilterConfig config) {
    this.config = config;
  }

  public FilterConfig getFilterConfig() {
    return config;
  }

  public void doFilter(ServletRequest req,
                       ServletResponse res,
                       FilterChain chain) {
    ServletContext context = getFilterConfig().getServletContext();
    long bef = System.currentTimeMillis();
    chain.doFilter(req, res);  // no chain parameter needed here
    long aft = System.currentTimeMillis();
    context.log("Request to " + req.getRequestURI() + ": " + (aft-bef));
  }
}

When the server calls

setFilterConfig()
, the filter saves a reference to the config in its config variable, which is later used in the
doFilter()
method to retrieve the
ServletContext
. The logic in
doFilter()
is simple; time how long request handling takes and log the time once processing has completed. To use this filter, you must declare it in the web.xml deployment descriptor using the <filter> tag, as shown below:


<filter>
  <filter-name>
    log
  </filter-name>
  <filter-class>
    LogFilter
  </filter-class>
</filter>

This tells the server a filter named

log
is implemented in the
LogFilter
class. You can apply a registered filter to certain URL patterns or servlet names using the <filter-mapping> tag:


<filter-mapping>
  <filter-name>log</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

This configures the filter to operate on all requests to the server (static or dynamic), just what we want for our logging filter. If you connect to a simple page, the log output might look like this:


Request to /index.jsp: 10

Lifecycle events
Servlet API 2.3's second most significant change is the addition of application lifecycle events, which let "listener" objects be notified when servlet contexts and sessions are initialized and destroyed, as well as when attributes are added or removed from a context or session.

Servlet lifecycle events work like Swing events. Any listener interested in observing the

ServletContext
lifecycle can implement the
ServletContextListener
interface. The interface has two methods:

The

ServletContextEvent
class passed to those methods has only a
getServletContext()
method that returns the context being initialized or destroyed.

A listener interested in observing the

ServletContext
attribute lifecycle can implement the
ServletContextAttributesListener
interface, which has three methods:

The

ServletContextAttributeEvent
class extends
ServletContextEvent
, and adds
getName()
and
getValue()
methods so the listener can learn about the attribute being changed. That is useful because Web applications that need to synchronize application state (context attributes) with something like a database can now do it in one place.

The session listener model is similar to the context listener model. In the session model, there's an

HttpSessionListener
interface with two methods:

The methods accept an

HttpSessionEvent
instance with a
getSession()
method to return the session being created or destroyed. You can use all these methods when implementing an admin interface that keeps track of all active users in a Web application.

The session model also has an

HttpSessionAttributesListener
interface with three methods. Those methods tell the listener when attributes change, and could be used, for example, by an application that synchronizes profile data held in sessions into a database:

As you might expect, the

HttpSessionBindingEvent
class extends
HttpSessionEvent
and adds
getName()
and
getValue()
methods. The only somewhat abnormal thing is that the event class is named
HttpSessionBindingEvent
, not
HttpSessionAttributeEvent
. That's for legacy reasons; the API already had an
HttpSessionBindingEvent
class, so it was reused. This confusing aspect of the API may be ironed out before final release.

A possible practical use of lifecycle events is a shared database connection managed by a context listener. You declare the listener in the web.xml as follows:


<listener>
  <listener-class>
    com.acme.MyConnectionManager
  </listener-class>
</listener>

The server creates an instance of the listener class to receive events and uses introspection to determine what listener interface (or interfaces) the class implements. Bear in mind that because the listener is configured in the deployment descriptor, you can add new listeners without any code change. You could write the listener itself as something like this:


public class MyConnectionManager implements

ServletContextListener
{

  public void contextInitialized(ServletContextEvent e) {
    Connection con = // create connection
    e.getServletContext().setAttribute("con", con);
  }

  public void contextDestroyed(ServletContextEvent e) {
    Connection con = (Connection) e.getServletContext().getAttribute("con");
    try { con.close(); } catch (SQLException ignored) { } // close connection
  }
}

This listener ensures that a database connection is available in every new servlet context, and that all connections are closed when the context shuts down.

The

HttpSessionActivationListener
interface, another new listener interface in API 2.3, is designed to handle sessions that migrate from one server to another. A listener implementing
HttpSessionActivationListener
is notified when any session is about to passivate (move) and when the session is about to activate (become live) on the second host. These methods give an application the chance to persist nonserializable data across JVMs, or to glue or unglue serialized objects back into some kind of object model before or after migration. The interface has two methods:

You register this listener just like the others. However, unlike the others, the passivate and activate calls here will most likely occur on two different servers!

Select a character encoding
API 2.3 provides much-needed support for handling foreign language form submittals. There's a new method,

request.setCharacterEncoding(String encoding)
, that lets you tell the server a request's character encoding. A character encoding, also known as a charset, is a way to map bytes to characters. The server can use the specified charset to correctly parse the parameters and POST data. By default, a server parses parameters using the common Latin-1 (ISO 8859-1) charset. Unfortunately, that only works for Western European languages. When a browser uses another charset, it is supposed to send the encoding information in the Content-Type header of the request, but almost no browsers do. This method lets a servlet tell the server what charset is in use (it is typically the charset of the page that contains the form); the server takes care of the rest. For example, a servlet receiving Japanese parameters from a Shift_JIS encoded form could read the parameters like this:


  // Set the charset as Shift_JIS
  req.setCharacterEncoding("Shift_JIS");

  // Read a parameter using that charset
  String name = req.getParameter("name");

Remember to set the encoding before calling

getParameter()
or
getReader()
. The
setCharacterEncoding()
call may throw
java.io.UnsupportedEncodingException
if the encoding is not supported. This functionality is also available for users of API 2.2 and earlier, as part of the
com.oreilly.servlet.ParameterParser
class. (See Resources.)

JAR dependencies
Often, a WAR file (Web application archive file, added in API 2.2) requires various other JAR libraries to exist on the server and operate correctly. For example, a Web application using the

ParameterParser
class needs cos.jar in the classpath. A Web application using WebMacro needs webmacro.jar. Before API 2.3, either those dependencies had to be documented (as if anyone actually reads documentation!) or each Web application had to include all its required jar files in its own WEB-INF/lib directory (unnecessarily bloating each Web application).

Servlet API 2.3 lets you express JAR dependencies within the WAR using the WAR's META-INF/MANIFEST.MF entry. That is the standard way for jar files to declare dependencies, but with API 2.3, WAR files must officially support the same mechanism. If a dependency can't be satisfied, a server can politely reject the Web application at deployment time instead of causing an obscure error message at runtime. The mechanism allows a high degree of granularity. For example, you can express a dependency on a particular version of an optional package, and the server has to find the right one with a search algorithm. (See Resources for a link to documentation that explains in detail how the manifest versioning model works.)

Class loaders
Here's a small change with a big impact: In API 2.3, a servlet container (a.k.a. the server) will ensure that classes in a Web application not be allowed to see the server's implementation classes. In other words, the class loaders should be kept separate.

That doesn't sound like much, but it eliminates the possibility of a collision between Web application classes and server classes. That had become a serious problem because of XML parser conflicts. Each server needs an XML parser to parse web.xml files, and many Web applications these days also use an XML parser to handle reading, manipulation, and writing of XML data. If the parsers supported different DOM or SAX versions, that could cause an irreparable conflict. The separation of class scope solves this issue nicely.

New error attributes
The previous API version, Servlet API 2.2, introduced several request attributes that could be used by servlets and JSPs acting as targets of an <error-page> rule. If you don't remember <error-page> rules, they let you configure a Web application so that certain error status codes or exception types cause specific pages to be displayed:


<web-app>
    <!-- ..... -->
    <error-page>
        <error-code>
            404
        </error-code>
        <location>
            /404.html
        </location>
    </error-page>
    <error-page>
        <exception-type>
            javax.servlet.ServletException
        </exception-type>
        <location>
            /servlet/ErrorDisplay
        </location>
    </error-page>
    <!-- ..... -->
</web-app>

A servlet in the <location> for an <error-page> rule could receive the following three attributes:

Using those attributes, a servlet could generate an error page customized to the error, as shown below:


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ErrorDisplay extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    String code = null, message = null, type = null;
    Object codeObj, messageObj, typeObj;

    // Retrieve the three possible error attributes, some may be null
    codeObj = req.getAttribute("javax.servlet.error.status_code");
    messageObj = req.getAttribute("javax.servlet.error.message");
    typeObj = req.getAttribute("javax.servlet.error.exception_type");

    // Convert the attributes to string values
    // We do things this way because some old servers return String
    // types while new servers return Integer, String, and Class types.
    // This works for all.
    if (codeObj != null) code = codeObj.toString();
    if (messageObj != null) message = messageObj.toString();
    if (typeObj != null) type = typeObj.toString();

    // The error reason is either the status code or exception type
    String reason = (code != null ? code : type);

    out.println("<HTML>");
    out.println("<HEAD><TITLE>" + reason + ": " + message + "</TITLE></HEAD>");
    out.println("<BODY>");
    out.println("<H1>" + reason + "</H1>");
    out.println("<H2>" + message + "</H2>");
    out.println("<HR>");
    out.println("<I>Error accessing " + req.getRequestURI() + "</I>");
    out.println("</BODY></HTML>");
  }
}

But what if the error page could contain the exception stack trace or the URI of the servlet that truly caused the problem (since it's not always the originally requested URI)? With API 2.2, that wasn't possible. With API 2.3, that information is available with two new attributes:

Those attributes let the error page include the stack trace of the exception and the URI of the problem resource. The servlet below has been rewritten to use the new attributes. (It fails gracefully if they don't exist, for backward compatibility.)

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ErrorDisplay extends HttpServlet {   public void doGet(HttpServletRequest req, HttpServletResponse res)                                throws ServletException, IOException {     res.setContentType("text/html");     PrintWriter out = res.getWriter();     String code = null, message = null, type = null, uri = null;     Object codeObj, messageObj, typeObj;     Throwable throwable;     // Retrieve the three possible error attributes, some may be null     codeObj = req.getAttribute("javax.servlet.error.status_code");     messageObj = req.getAttribute("javax.servlet.error.message");     typeObj = req.getAttribute("javax.servlet.error.exception_type");     throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");     uri = (String) req.getAttribute("javax.servlet.error.request_uri");     if (uri == null) {       uri = req.getRequestURI();  // in case there's no URI given     }     // Convert the attributes to string values     if (codeObj != null) code = codeObj.toString();     if (messageObj != null) message = messageObj.toString();     if (typeObj != null) type = typeObj.toString();     // The error reason is either the status code or exception type     String reason = (code != null ? code : type);     out.println("<HTML>");     out.println("<HEAD><TITLE>" + reason + ": " + message + "</TITLE></HEAD>");     out.println("<BODY>");     out.println("<H1>" + reason + "</H1>");     out.println("<H2>" + message + "</H2>");     out.println("<PRE>");     if (throwable != null) {       throwable.printStackTrace(out);     }     out.println("</PRE>");     out.println("<HR>");     out.println("<I>Error accessing " + uri + "</I>");     out.println("</BODY></HTML>");   } }

New security attributes
Servlet API 2.3 also adds two new request attributes that can help a servlet make an informed decision about how to handle secure HTTPS connections. For requests made using HTTPS, the server will provide these new request attributes:

A servlet can use those attributes to programmatically decide if the connection is secure enough to proceed. An application may reject connections with small bitsizes or untrusted algorithms. For example, a servlet could use the following method to ensure that its connection uses at least a 128-bit key size.


public boolean isAbove128(HttpServletRequest req) {
  Integer size = (Integer) req.getAttribute("javax.servlet.request.key_size");
  if (size == null || size.intValue() < 128) {
    return false;
  }
  else {
    return true;
  }
}

Note: The attribute names in the Proposed Final Draft use dashes instead of underscores; however, they're being changed, as shown here before the Final Release, to be more consistent with existing attribute names.

Little tweaks
A number of small changes also made it into the API 2.3 release. First, the

getAuthType()
method that returns the type of authentication used to identify a client has been defined to return one of the four new static final String constants in the
HttpServletRequest
class:
BASIC_AUTH
,
DIGEST_AUTH
,
CLIENT_CERT_AUTH
, and
FORM_AUTH
. This allows simplified code like:


if (req.getAuthType() == req.BASIC_AUTH) {
  // handle basic authentication
}

Of course, the four constants still have traditional String values, so the following code from API 2.2 works too, but is not as fast or as elegant. Notice the reverse

equals()
check to avoid a
NullPointerException
if
getAuthType()
returns null:


if ("BASIC".equals(req.getAuthType())) {
  // handle basic authentication
}

Another change in API 2.3 is that

HttpUtils
, also known as "the class that never should have been made public," has been deprecated.
HttpUtils
has always stood out as an odd collection of static methods -- calls that were useful sometimes, but might have been better placed elsewhere. In case you don't recall, the class contained methods to reconstruct an original URL from a request object and to parse parameter data into a hashtable. API 2.3 moves this functionality into the request object where it more properly belongs, and deprecates
HttpUtils
. The new methods on the request object are:

API 2.3 also adds two new methods to

ServletContext
that let you obtain the name of the context and a list of all the resources it holds:

There's also a new method on the response object to increase programmer control of the response buffer. API 2.2 introduced a

res.reset()
method to reset the response and clear the response body, headers, and status code. API 2.3 adds a
res.resetBuffer()
that clears just the response body:

And finally, after a lengthy debate by a group of experts, Servlet API 2.3 has clarified once and for all exactly what happens on a res.sendRedirect("/index.html") call for a servlet executing within a non-root context. The issue is that Servlet API 2.2 requires an incomplete path like "/index.html" to be translated by the servlet container into a complete path, but doesn't say how context paths are handled. If the servlet making the call is in a context at the path "/contextpath," should the redirect URI translate relative to the container root (http://server:port/index.html) or the context root (http://server:port/contextpath/index.html)? For maximum portability, it's imperative to define the behavior; after lengthy debate, the experts chose to translate relative to the container root. For those who want context relative, you can prepend the output from

getContextPath()
to your URI.

DTD clarifications
Finally, Servlet API 2.3 ties up a few loose ends regarding the web.xml deployment descriptor behavior. It's now mandated that you trim text values in the web.xml file before use. (In standard non-validated XML, all white space is generally preserved.) This rule ensures that the following two entries can be treated identically:


<servlet-name>hello<servlet-name>

and


<servlet-name>
  hello
</servlet-name>

API 2.3 also allows an <auth-constraint> rule, so the special value "*" can be used as a <role-name> wildcard to allow all roles. That lets you write a rule, like the following, that lets all users enter as soon as they've been properly identified as belonging to any role in the Web application:


<auth-constraint>
  <role-name>*</role-name>  <!-- allow all recognized roles -->
</auth-constraint>

Lastly, it's been clarified that you can use a role name declared by a <security-role> rule as a parameter to the

isUserInRole()
method. For example, with the following snippet of a web.xml entry:


<servlet>
    <servlet-name>
        secret
    </servlet-name>
    <servlet-class>
        SalaryViewer
    </servlet-class>
    <security-role-ref>
        <role-name>
            mgr        <!-- name used by servlet -->
        </role-name>
        <role-link>
            manager    <!-- name used in deployment descriptor -->
        </role-link>
    </security-role-ref>
</servlet>

<!-- ... -->

<security-role>
    <role-name>
        manager
    </role-name>
</security-role>

the servlet

secret
can call
isUserInRole("mgr")
or
isUserInRole("manager")
-- they will give the same behavior. Basically,
security-role-ref
acts to create an alias, but isn't necessary. That is what you'd naturally expect, but the API 2.2 specification could be interpreted as implying that you could only use roles explicitly declared in a <security-role-ref> alias rule. (If that doesn't make sense to you, don't worry about it; just be aware that things are now guaranteed to work as they should.)

Conclusion
As I've described in this article, Servlet API 2.3 includes an exciting new filter mechanism, an expanded lifecycle model, and new functionality to support internationalization, error handling, secure connections, and user roles. The specification document has also been tightened to remove ambiguities that could interfere with cross-platform deployment. All in all, there are 15 new classes (most involving the new lifecycle event model, the others involving filters), four methods added to existing classes, four new constant variables, and one deprecated class. For a cheat sheet on moving from 2.2 to 2.3, see the sidebar.






Cheat Sheet for Servlet API 2.3

New Classes

Filter, FilterChain, FilterConfig, ServletContextAttributeEvent, ServletContextAttributesListener, ServletContextEvent, ServletContextListener, ServletRequestWrapper, ServletResponseWrapper, HttpServletRequestWrapper, HttpServletResponseWrapper, HttpSessionActivationListener, HttpSessionAttributesListener, HttpSessionEvent, HttpSessionListener

New Methods

getResourcePaths(), getServletContextName(), resetBuffer(), HttpSessionBindingEvent.getValue()

New Constants

BASIC_AUTH, DIGEST_AUTH, CLIENT_CERT_AUTH, and FORM_AUTH

Deprecated Classes

HttpUtils