Servlet Basics
The Servlet API consists of two packages, javax.servlet
and javax.servlet.http
. The javax
is left over from an earlier stage of
Java package naming conventions. As mentioned (and as indicated by the
fact that this chapter appears in Part I of this book), servlets are
a standard part of J2EE.
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, a static HTML file. On the server side, however, there is
Figure 3-1. The servlet lifecycle
an important difference: persistence.[7] Instead of shutting down at the end of each request, the servlet can remain loaded, ready to handle subsequent requests. Figure 3-1 shows how this all fits together.
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 is that you incur most of the startup overhead only once. Most of the I/O-intensive resources (such as database connection pools) your application will need can be created by the servlet ...
Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.