Genericservlet,httpservletrequest and HttpServletResponse

Source: Internet
Author: User

The most basic is to write the Servlet class by implementing a servlet interface, which requires implementing the 5 methods defined in the Servlet interface.

To simplify the writing of Servlets, an abstract class Genericservlet is provided in the Javax.servlet package, which gives a simple implementation of 4 other methods besides the service () method.

If you are writing a generic servlet, you only need to inherit from the Genericservlet class and implement the abstract Method service ().

In the Genericservlet class, two overloaded init () methods are defined:
public void init (ServletConfig config) throws Servletexception
public void Init () throws Servletexception

The first init () method is the implementation of the Init () method in the Servlet interface. In this method, the ServletConfig object is first saved in a transient instance variable, and then the second init () method without parameters is called.

Typically, when you write a servlet class that inherits from Genericservlet, you only need to rewrite the second init () method without parameters.
If the first init () method is overridden, then a call to the Super.init (config) code should be included in the method of the child class.

Most Web applications use the HTTP protocol to access server-side resources, and the servlet is written primarily for requests and responses to the HTTP protocol.
For rapid development, an abstract class HttpServlet is provided directly in the Javax.servlet.http package, which inherits from the Genericservlet class.

Public abstract class HttpServlet extends Genericservlet implements Java.io.Serializable

Two overloaded service () methods are available in HttpServlet:

public void Service (ServletRequest req, servletresponse Res) throws Servletexception, Java.io.IOException

protected void Service (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, Java.io.IOException

The first service () method is an implementation of the service () method in the Genericservlet class. In this method, the first love of the Req and Res object is converted to HttpServletRequest (inherited from the ServletRequest interface) and HttpServletResponse (inherit from the Servletresponse interface) type, The second service method is then called to process the customer request.

7 processing methods are provided for the 7 request methods Get,post,head,put,delete,trace and Options,httpservlet defined in HTTP1.1:

protected void Doget (Httpserveletrequest req, HttpServletResponse resp) throws Servletexception, Java.io.IOException

protected void DoPost (Httpserveletrequest req, HttpServletResponse resp) throws Servletexception, Java.io.IOException

protected void Dohead (Httpserveletrequest req, HttpServletResponse resp) throws Servletexception, Java.io.IOException

protected void DoPut (Httpserveletrequest req, HttpServletResponse resp) throws Servletexception, Java.io.IOException

protected void DoDelete (Httpserveletrequest req, HttpServletResponse resp) throws Servletexception, Java.io.IOException

protected void Dotrace (Httpserveletrequest req, HttpServletResponse resp) throws Servletexception, Java.io.IOException

protected void Dooptions (Httpserveletrequest req, HttpServletResponse resp) throws Servletexception, Java.io.IOException

The parameter types and exception throw types of the 7 methods are consistent with the service () method of the second overload in the HttpServlet class. When a container receives a request for an HttpServlet object, the order of the methods in the object is called as follows:

1. Call the public service () method.
2. In the public Service () method, first convert the parameter type to HttpServletRequest and HttpServletResponse, then call the protected (protected) service () method, The converted HttpServletRequest object and the HttpServletResponse object are passed in as parameters.
3. In the protected service () method, first call the HttpServletRequest object's GetMethod () method, get the name of the HTTP request method, and then call the corresponding Doxxx () method based on the type of the requested method.

Therefore, when you write a derived class of httpservlet, you do not need to overwrite the service () method, and you only need to override the corresponding Doxxx () method.

Although HttpServlet is an abstract class, there is no abstract method in this class, and all of the methods have been implemented. Just how to deal with customer requests in this class is not really implemented, and it is impossible to really implement it, because the customer request how to deal with, need to according to the actual application to decide. Rewrite the request handling method as required by your application.

HttpServletRequest and HttpServletResponse
The two interfaces, HttpServletRequest and HttpServletResponse, inherit from the Javax.servlet.ServletRequest and Javax.servlet.ServletResponse interfaces, respectively.
There are a lot of methods in these two interfaces, which are not discussed here.

Genericservlet,httpservletrequest and HttpServletResponse

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.