Edit Edit

Java Servlet FAQ


What's the general structure of a servlet?


The base class for all servlets is the GenericServlet class, which is not protocol-specific. The main functions of GenericServlet are:

Method
Description
public void init(ServletConfig config)called when the servlet is loaded
public void service(ServletRequest req)called for each client request
public void destroy()called when the servlet is unloaded

The HttpServlet class, which supports the HTTP protocol, extends GenericServlet. The main functions are:

Method
Description
public void init(ServletConfig config)called when the servlet is loaded
void doGet(HttpServletRequest req, HttpServletResponse res)called for each HTTP GET request
void doPost(HttpServletRequest req, HttpServletResponse res)called for each HTTP POST request
public void destroy()called when the servlet is unloaded

HttpServlet's service() method parses the client request and forwards the client request to the appropriate HTTP method. For example, if an HTTP GET request comes into service(), service() preprocesses the GET request and then forwards it to doGet().

Any servlet class that you write should extend either the GenericServlet or HttpServlet class. Servlets that support the HTTP protocol should extend the HttpServlet class and servlets that support a different protocol should extend the GenericServlet class. Most of the time, you will write a servlet that extends HttpServlet.



Last updated 12/25/1999 09:21:01 PM