Java Reference
In-Depth Information
Servlets are efficient because they run within the process of the servlet
container, much as an applet runs within a browser. A process is a term for a
program that is executing. A servlet is loaded with the first request and then
remains loaded to handle subsequent requests. To handle multiple requests
within a single process, servlets are multithreaded. Multithreading involves
using the same instance of a process for multiple, concurrent uses, each known
as a thread of execution , or thread . Each request causes the servlet container to
create a separate thread within the single process. This is more efficient because
starting a process uses more system resources than creating a thread. Using
threads effectively is a more complex task for the programmer because the code
must be made thread-safe. Code that is thread-safe avoids unwanted interaction
between the threads, so the actions of one thread will not interfere with the
actions of another. Because threads run the same instance of a program, each
thread gets its own copy of local variables, but all threads of an instance share
the same instance variables.
When a request is passed to a Web server running a servlet container (for
example, the Tomcat Web server), the request is forwarded to the servlet con-
tainer, which determines whether a static or dynamic resource was requested.
Static resources, such as a static Web page, are opened, read, and returned to the
client by a special class within the container called the DefaultServlet class. If a
dynamic resource such as a servlet is requested, the servlet either is loaded
directly or a class called, InvokerServlet, will load and execute the servlet.
The Servlet Life Cycle
Every servlet must implement specific methods while it executes. The
methods init() and destroy() are provided to manage resources held for the life
of the servlet. The init() method is called when a servlet is first loaded, similar to
the init() method for an applet. The destroy() method is called when the servlet
container determines that the servlet should be removed from service.
Between the init() and destroy() methods, the service() method is called
each time a request is received. The HttpServlet class implements the service()
method and defines helper methods, doGet() and doPost(), for Get and Post
requests, respectively. The HttpServlet service() method routes each request to
the appropriate method. A servlet using HTTP needs only to extend HttpServlet
and override the desired methods. Typically, a servlet will override only doGet()
or doPost(), depending on the type of request to which it responds, although
both may be overridden. If resources, such as a database connection, must be
obtained for the life of the servlet and then released, the init() and destroy()
methods also are overridden to ensure that the resources are maintained for the
life of the servlet.
Recall that line 39 in Figure 12-7 on page 787 of the code for the initial Web
page, index.html, sends form data to a servlet named WebStocks. The WebStocks
servlet is the controller in the MVC model for this application. As such, most of
the Web pages in this application either submit data to this servlet or are
directed by this servlet for display to the user, or both. In some cases, the servlet
actually creates the Web page; however, this is done only for illustration as it vio-
lates the MVC model. Figure 12-14 displays the code used to start creating the
WebStocks servlet.
Search WWH ::




Custom Search