Servlet classes must implement the javax.servlet.Servlet interface. The Servlet API provides two classes that implement this interface, which you can extend to create Java servlets:
GenericServlet Provides basic, protocol-independent, servlet functionality. You extend this class to code non-HTTP services. In most cases, however, your classes extend from HttpServlet.
HttpServlet Extends GenericServlet to add HTTP-specific functionality. Most of your classes extend HttpServlet, and the discussions in this chapter assume that your classes perform HTTP processing.
The Servlet API uses classes and interfaces from the java.io, java.lang, javax.servlet, and javax.http.servlet packages, as the following figure shows:
For complete information on the classes, interfaces, and exceptions in the Servlet API, see the Servlet API JavaDocs.
Servlets undergo a multiphase lifecycle, as the following figure shows:
During the initialization phase, JRun invokes the servlet's init method. The init method gets called only once, thus avoiding the need to allocate expensive resources every time the JRun invokes the servlet. Use the init method to allocate resources the servlet requires throughout its lifecycle. JRun invokes the init method at startup (if enabled in the JMC) or when the first client request is received.
You are not required to override the init method in your servlet class. You override it only when you want to perform some type of processing at initialization time.
The servicing requests phase occurs when the servlet handles all incoming requests. JRun routes requests to either the service method if you override Generic Servlet) or a doXxx method (if you override the HttpServlet class).
JRun unloads the servlet from memory when you shut down the JRun server or when the JRun server reloads a modified servlet. At the time of the unload, the servlet goes through the destroy phase. When the destroy request is initiated, JRun calls the destroy method. Inside the destroy method, you can deallocate resources that the servlet was using. As soon as JRun invokes the destroy method, the class can be garbage-collected.
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/jrun/4/Programmers_Guide/techniques_servlet2.htm
Comments
No screen name said on Nov 29, 2004 at 7:56 PM :