Let's talk about exceptions ...
How do you handle exceptions? Do you think upfront about the type of exceptions that you want to catch or do you just let the outside world handle it?

-- Jeroen van Bergen in JW Blogs

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Build servlet-based enterprise Web applications

Learn to build better, faster servlets with advanced servlet techniques

The Java servlet architecture provides an excellent framework for server-side processing. Because servlets are written in Java, they can take advantage of Java's memory management and rich set of APIs. Servlets can also run on numerous platforms and HTTP servers without change. Further, the servlet architecture solves the major problem of the CGI architecture: a servlet request spawns a lightweight Java thread, while a CGI request will spawn a complete process. Unless the project has a large budget for high-end Java-based application server projects, servlets offer the best technology to develop server-side components and Web-based applications.

This article, however, is not about the many advantages of the servlet architecture (see Resources for articles covering that); rather, it is about how to use servlets to generate responses efficiently, use less memory in the process, and stream the generated responses back to the client, making response times seem even better. Applying the techniques in this article will allow you to build servlets that can serve more requests and serve them up faster. Based on that foundation, you can build enterprise Web applications that will scale to your needs, run 24x7, and provide the best response times.

To aid my discussion, I use a simple example servlet that counts down from 10 to 1. Each example of the countdown servlet is slightly modified to demonstrate a specific technique. By focusing on the techniques, rather than a fancy example, you'll see how to apply the specific techniques to solve problems in your own servlets.

Note: This article assumes you're familiar with the servlet architecture and have experience writing servlets. See Resources for tutorial information.

Effective servlets

There are two major goals to building servlets for efficient HTML generation. The first goal, especially important for high-volume Web applications, is to carefully control the amount of garbage created during page generation. The more garbage created, the fewer pages generated, and the slower the response time for a request. This bottleneck occurs because the Java VM has to steal cycles to run garbage collection when it should be generating pages. If too much garbage is created, request processing could halt entirely. This is unacceptable for critical Web applications. These problems can be largely avoided using simple ecological techniques: reduce, reuse,and recycle. We'll discuss these techniques in more detail in just a moment.

The second goal of efficient HTML generation is to keep the browser active: the typical Web surfer doesn't have much tolerance for blank gray pages and "waiting for response" messages. The time a Web application spends querying a database or accessing a legacy system is typically the time a user spends staring at a blank page. This can -- and should -- be avoided using servlet response streaming techniques. As we'll see, these techniques dovetail well with the aforementioned ecological techniques, and together they form a foundation for building solid, well-behaved servlets.

Resources
  • Download the complete source for the examples in this article http://www.javaworld.com/jw-12-1998/servlethtml/jw-12-servlethtml.zip
  • Sun's Servlet Web site http://www.javasoft.com/products/servlet/index.html
  • Servlet Specification version 2.1 http://java.sun.com/products/servlet/2.1/html/servletapiTOC.fm.html
  • Sun's servlet tutorial http://www.javasoft.com/docs/books/tutorial/servlets/index.html
  • Jason Hunter's "Introducing the new Servlet API 2.1" (JavaWorld, December 1998) explains the differences between 2.0 and 2.1, discusses the reasons for the changes, and shows you how to write servlets using the new release http://www.javaworld.com/jw-12-1998/jw-12-servletapi.html
  • Servlets.com is a companion site to Jason Hunter's Java Servlet Programming (O'Reilly, November 1998) http://www.servlets.com
  • O'Reilly provides information detailed information about the book http://www.oreilly.com/catalog/jservlet
  • Alex Chaffee's Servlet FAQ answers common questions about servlets http://www.purpletech.com/java/servlet-faq
  • See JavaWorld's list of articles about server-side Java http://www.javaworld.com/common/jw-ti-ssj.html
  • Check out ServletCentral, a servlet-specific online magazine http://www.servletcentral.com
  • The ServletSource has a number of good servlet tutorials and examples http://www.servletsource.com