Java Reference
In-Depth Information
The service() method implements a request and response paradigm. The ServletRequest
object contains information about the service request, encapsulating information provided by
the client. The ServletResponse object contains the information returned to the client.
The destroy() Method
This method signifies the end of a servlet's life. When a service is being shut down it calls the
servlet's destroy() method. This is where any resources that were created in the init()
method should be cleaned up. If you have an open database connection, you should close it
here. This is also a good place to save any persistent information that will be used the next
time the servlet is loaded. The signature of destroy() is very simple, but I have displayed it
here just to complete the picture:
public void destroy();
A Basic Servlet
In this section, we are going to look at building a very basic servlet. Its purpose will be to ser-
vice a request and respond with the request method used by the client. We will take a quick
look at the servlet's source code, the steps involved in compiling and installing the servlet, and
the HTML necessary to invoke the servlet. After you have the servlet compiled, you will need
to refer to Appendix A, “Web Applications and Configuring the Servlet Engine,” for instruc-
tions on how to configure the servlet engine, which actually runs your servlets.
3
The BasicServlet Source
Listing 3.1 contains the source code for this example. You can find the following source listing
on this topic's Web site. If you have the time, it is probably best to type the first few examples
yourself. This will help you become familiar with the basic parts of servlets. As you type or
browse over this listing, feel free to look up the referenced classes in Appendices A and B.
L ISTING 3.1
BasicServlet.java Displays the REQUEST_METHOD used by the Client
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class BasicServlet extends HttpServlet {
public void init(ServletConfig config)
throws ServletException {
 
Search WWH ::




Custom Search