Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
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.
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.