General Form of an HttpServlet Subclass


General Form of an HttpServlet Subclass

To create an HTTP servlet, you must first create a subclass of HttpServlet . This process is a fairly simple matter because of inheritance. An HttpServlet subclass will inherit all of the methods declared in the HttpServlet class. The subclass only needs to override the methods the subclass will use. For example, the following is the general form for an HttpServlet subclass that will respond to HTTP GET commands.

 import javax.servlet.*; import javax.servlet.http.*; public class MyServlet           extends HttpServlet {   public void doGet(         HttpServletRequest request,         HttpServletResponse response)            throws ServletException,                   IOException   {     //  code to extract and parse inputs     //  code to run application     //  code to return output to client   } } 

This servlet class only concerns itself with GET commands, so it only overrides the doGet() method. If any other HTTP commands are received from the client, the HttpServlet class stub methods are called and nothing happens. There is no reason that a servlet class couldn't override more than one of the HttpServlet class methods. The import declarations at the top of the program give the MyServlet class access to the classes and interfaces contained in the javax.servlet and javax.servlet.http packages.

The code inside the doGet() method will perform three functions. It will extract and parse the input parameters that came with the client request, it will run the desired application, and it will send the output from the application back to the client machine. Now for the details.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net