What's new in Java Servlet API 2.2?

A full update on the latest Java Servlet API

1 2 3 Page 3
Page 3 of 3

There's also a new getRequestDispatcher(String path) method in ServletRequest. This method can accept a relative URL target, unlike the getRequestDispatcher() method in ServletContext that only accepts a fully qualified URL. The relative path must still resolve to a component in the current context (that is, the same Web app); for dispatching to another Web app, you must get a handle to the other Web app context and use that handle to locate the RequestDispatcher.

Lastly, the sendRedirect(String url) method in HttpServletResponse has been changed so that it now supports relative URLs. Previously, the URL passed to sendRedirect() had to be absolute (beginning, for example, with "http://") because the HTTP specification dictates that all redirect URLs must be absolute. Of course, the HTTP spec didn't change for 2.2, but now sendRedirect() is smart enough to convert any relative URL passed in to an absolute URL on its way to the client. Snazzy.

Conclusion

As described in this article, Java Servlet API 2.2 includes many enhancements designed to make servlets part of a complete Web application framework. Whole subsections of Web sites will be able to be configured and deployed to any Web server that supports API 2.2. Servlets can also be distributed across multiple back-end servers, or can indicate via their deployment descriptor that they'd rather stick to one machine. Response buffering has been added to make error handling more robust, HTTP header control has been improved, and request dispatching has been made easier with support for relative paths and named dispatchers.

All in all, there are 29 new methods, 2 new constants, and just 6 deprecated methods. For a cheat sheet on moving from 2.1 to 2.2, see the sidebar.

Jason Hunter works as the Chief Technology Officer of K&A Software, where he specializes in Java training and consulting. He is author of Java Servlet Programming and publisher of the Web site on servlets at http://www.servlets.com. He belongs to the working group responsible for Java Servlet API development (and has his fingerprints all over the 2.2 specification and reference implementation). If, by some miracle you don't find him at work, he's probably out hiking in the mountains. (It'll be quite the miracle, too, since he's actively working on updating Java Servlet Programming for Java Servlet API 2.2.)

Learn more about this topic

1 2 3 Page 3
Page 3 of 3