1. Introduction to servelet

Source: Internet
Author: User

I. Overview

Servlet is a server-side Java application independent of platform and protocol. It can generate dynamic web pages.

Servlet is a Java application on the server inside the Web server. Unlike the traditional Java application started from the command line, servlet is loaded by the Web server, the Web server must contain a Java virtual machine that supports servlet. The structure is shown in:

 

Servlet mainly completes the following work:

1. Read data sent from the client

2. Read the data requested by the browser

3. generate results

4. send data to the client

5. Send HTTP Response Data

 

Ii. httpservlet type

The httpservlet class is an abstract class that extends the genericservlet class. The httpservlet class is used to create a servlet that is applicable to web sites and supports HTTP. The subclass of an httpservlet must at least reload one of the following methods.
The doget () method is applicable to http get requests.
The dopost () method is applicable to http post requests.
The doput () method is applicable to http put requests.
The dodelete () method is applicable to HTTP Delete requests.
Init () and destroy () methods to manage resources in the servlet lifecycle.
The getservletinfo () method provides Servlet Information.

 

Doget ()Method Overview: protected void doget (httpservletrequest req, httpservletresponse resp) throws servletexception, java. Io. ioexception
The server calls (through the Service () method) to allow a servlet to process a GET request. Override this method to support a GET request and automatically support an HTTP head request. A head request is a GET request. In the response, only the request header region is returned, and no body region is returned. When overwriting this method, read the request data, write the response header, obtain the writer or output stream object of the response, and write the response data. It is best to include content type and encoding (encoding ). When a printwriter object is used to return a response, you must set the content type before accessing the printerwriter object. The servlet container must write the header information before submitting the response, because in HTTP, the header information must be transmitted before the body is sent. If possible, set the Content-Length (using the servletresponse. setcontentlength (INT) method) of the header information to allow the servlet container to return a response to the client using a persistent connection, thus improving performance. If the entire response is suitable for storing in the Response Buffer, Content-Length is automatically set. The get method is secure. If the client request needs to change the stored data, use other HTTP methods.
The get method shoshould also be idempotent, meaning that it can be safely repeated. sometimes making a method safe also makes it idempotent. for example, repeating queries is both safe and idempotent, but buying a product online or modifying data is neither safe nor idempotent. (translation unavailable currently)
If the request format is incorrect, doget returns an HTTP "Bad request" message.

 

Dohead ()Method Overview: protected void dohead (httpservletrequest req, httpservletresponse resp) throws servletexception, java. Io. ioexception
Returns an HTTP head request from a protected service () method and processes the request. When the client only needs to know the response header, such as content-type or Content-Length, the client only needs to send a head request. HTTP head accurately calculates the number of output bytes to set the Content-Length.
If you override this method, you can avoid calculating the response body. Instead, you only need to set the response header to improve performance.

 

Dopost ()Method Overview: protected void dopost (httpservletrequest req, httpservletresponse resp) throws servletexception, java. Io. ioexception
The server calls (through the Service () method) to allow a servlet to process a POST request. The http post method allows customers to send unlimited data to the server at the same time, and it seems that credit card numbers and so on are allowed. When this method is overwritten, the system reads the request data, writes the response header, obtains the writer or output stream object of the response, and finally writes the response data. It is best to include content type and encoding (encoding ). When a printwriter object is used to return a response, you must set the content type before accessing the printerwriter object. When HTTP 1.1 is used for encoding (this means that the response carries a transfer-encoding header), do not set the Content-Length header.

 

Doput ()Method Overview: protected void doput (httpservletrequest req, httpservletresponse resp) throws servletexception, java. Io. ioexception
The server calls (through the Service () method) to allow a servlet to process a put request. The put operation allows users to place files on the server as if they were using FTP.

 

Dodelete ()Method Overview: protected void dodelete (httpservletrequest req, httpservletresponse resp) throws servletexception, java. Io. ioexception
The server calls (through the Service () method) to allow a servlet to process a Delete request. The delete operation allows the customer to delete a document or webpage from the server.

 

Dooptions ()Method Overview: protected void dooptions (httpservletrequest req, httpservletresponse resp) throws servletexception, java. Io. ioexception
The server calls (through the Service () method) to allow a servlet to process an options request. The options operation determines which HTTP method the server supports and returns an appropriate header. For example, if a servlet overwrites the doget () method, the dooptions () method returns the following header information: Allow: Get, Head, Trace, options

 

Dotrace ()Method Overview: protected void dotrace (httpservletrequest req, httpservletresponse resp) throws servletexception, java. Io. ioexception
The server calls (through the Service () method) to allow a servlet to process a trace request. This method is used for program debugging without being overwritten.

Protected Service ()Method Overview: protected void Service (httpservletrequest req, httpservletresponse resp) throws servletexception, java. Io. ioexception
Receive standard HTTP requests from the public service () method and assign them to the doxxx member method. This method does not need to be overwritten.

 

Public Service ()Method Overview: assign a customer request to the protected service () method, which does not need to be overwritten.

 

 

Refer:

Http://www.cnblogs.com/johnny/articles/19688.aspx

 

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.