O'Reilly logo

Java Enterprise in a Nutshell, Second Edition by David Flanagan, Jim Farley, William Crawford

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Servlet Basics

The Servlet API consists of two packages, javax.servlet and javax.servlet.http. The javax is there because servlets are a standard extension to Java, rather than a mandatory part of the API. This means that while servlets are official Java, Java virtual machine developers aren’t required to include the classes for them in their Java development and execution environments. As mentioned already, however, the Servlet API is required for J2EE 1.3

The Servlet Lifecycle

When a client makes a request involving a servlet, the server loads and executes the appropriate Java classes. Those classes generate content, and the server sends the content back to the client. In most cases, the client is a web browser, the server is a web server, and the servlet returns standard HTML. From the web browser’s perspective, this isn’t any different from requesting a page generated by a CGI script, or, indeed, standard HTML. On the server side, however, there is an important difference: persistence.[16] Instead of shutting down at the end of each request, the servlet can remain loaded, ready to handle subsequent requests. Figure 5-1 shows how this all fits together.

The servlet lifecycle

Figure 5-1. The servlet lifecycle

The request-processing time for a servlet can vary, but it is typically quite fast when compared to a similar CGI program. The real performance advantage of a servlet, however, is that you incur ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required