About Httpservlet.service () steps

Source: Internet
Author: User

About Httpservlet.service () steps

About () method
   1. The HTTP Servlet uses an HTML table to send and receive data. To create an HTTP Servlet, you need to extend the HttpServlet class, which is a subclass of the genericservlet that handles HTML tables in a specialized way. When the form information is submitted, it specifies which servlet (or other program) the server should execute. The HttpServlet class contains methods such as Init (), Destroy (), service (), where the Init () and Destroy () methods are inherited.
2. HTTP ServletService () method

the Service () method is the core of the Servlet.

Whenever a client requests a HttpServlet object, the service () method of the object is called and passed to the method a "request" (ServletRequest) object and a "response" (Servletresponse) object as a parameter.

The service () method already exists in HttpServlet.The default service function is to invoke the DO function corresponding to the method of the HTTP request.
For example, if the HTTP request method is GET, Doget () is called by default.

The service method is the method in the interface, the servlet container sends all requests to the method, and the default behavior is to forward the HTTP request to the Doxxx method, if you overload the method, the default action is overwritten and no more forwarding is done!
Service () is defined in the Javax.servlet.Servlet interface, implemented in Javax.servlet.GenericServlet, and Doget/dopost is Implemented in Javax.servlet.http.HttpServlet, Javax.servlet.http.HttpServlet is a subclass of Javax.servlet.GenericServlet.

the servlet should overwrite the Do function for the HTTP method supported by the servlet. Because the Httpservlet.service () method checks whether the request method invokes the appropriate processing method, it does not have to overwrite the service () method。 Just overwrite the appropriate do method.

The DoPost () method is called when a client issues an HTTP POST request through an HTML form. The parameters associated with the POST request are sent from the browser to the server as a separate HTTP request.you should use the Dopost () method when you need to modify the server-side data.
When a customer passes HTMLwhen a form issues an HTTP GET request or directly requests a URL,The Doget () method is called. The parameters associated with the GET request are added to the URL and sent with the request.The doget () method should be used when the server-side data is not modified.

We also need to put the servletmade to handle both get requests and post requests(Eg:servlet to handle both get and post two requests),This only requirescalling the Doget method in the Dopost method, or overwriteService Methods (service method calls Doget, DoPost, Dohead, and so on)。This is a standard approach in real-world programming because it requires little extra work, but increases the flexibility of client-side coding.
Note: The same parameter table overloads of the parent class method are generally implemented in service (), that is, write the Super.service (req, resp);
Even with the service () method, the Doget () and Dopost () methods must be identified!!
(I have to ask for some advice ~ ~)


the response of the servlet can be:
An output stream, which is interpreted by the browser based on its content type (such as text/html);
One HTTP error response, redirected to another url,servlet,jsp

About Httpservlet.service () steps

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.