Servlet comprehension Notes [original]

Source: Internet
Author: User

Learning steps: 1. Understand how to use Servlet and learn about it from the sensibility. 2. Learn more about its mechanism. 3. Understand the implementation at the code level

Take notes on the Java Servlet API process:

Javax. servlet. HTTP. httpservlet, which inherits the genericservlet class and implements Java. io. serializable interface, which is an abstract class and creates an HTTP servlet for its subclass to adapt to a website. The subclass of httpservlet must rewrite at least one method. The methods that are frequently modified include:

1. If you want the servlet to Support http get requests, rewritedoGetMethod

2. If you want the servlet to Support http post requests, rewrite the dopost method.

3. If you want the servlet to not only process data, but also process files and rewrite the doput method. The put call is similar to post. It allows the client to store real files on the server, not just to transmit data.

4. If you want the servlet to allow the client to delete files or web pages on the server, rewrite the dodelete method, which is similar to put.

5. If you want to control the resources generated by the servlet lifecycle, you can rewrite init andDestroy Method

6. If you want the servlet to provide relevant information, you can call or override the getservletinfo method of the javax. servlet. http. httpservlet class to inherit the javax. servlet. genericservlet class.

In general, we will not rewrite the service method in the javax. servlet. http. httpservlet class. Because the service is a processing method for standard HTTP requests, the Service assigns the corresponding processing method based on the type of each standard HTTP request (the method starting with do as described above ). Similarly, we generally do not rewrite the dooptions and dotrace methods. The following three methods are described by the way:

I. Service

Protected void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception;

Public void Service (servletrequest request, servletresponse response) throws servletexception, ioexception;

This is a Servlet's HTTP-specific scheme, which distributes requests to other methods that support this request in this class.

Ii. dooptions

Protected void dooptions (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception;

It is called by the service method of the javax. servlet. http. httpservlet class to process an HTTP option operation. This operation automatically determines which HTTP method is supported. For example, if a servlet writes a subclass of httpservlet and overrides the doget method, dooption returns the following header: Allow: Get, Head, Trace, options

Iii. dotrace

Protected void dotrace (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception;

It is called by the service method of the javax. servlet. http. httpservlet class to process an http trace operation. The default execution result of this operation is to generate a response that contains information that reflects all the header fields sent in the trace request. When developing a servlet, You need to reload this method in most cases.

Many servlets often run on multi-threaded servers, which means that a servlet must process the current request and carefully synchronize the access to achieve resource sharing. Shared resources include data in memory (such as instances and class variables) and external objects (such as files, database connections, and network connections ). For more information about how to process multiple threads in a Java program, see Java tutorial on multithreaded programming.

Only one constructor of the javax. servlet. http. httpservlet class is public httpservlet (). This method does not need to be processed, because this class is an abstract class.

/*************************************** **************************************** ***************************************/

Before you fully understand every method of the javax. servlet. http. httpservlet class, let's take a look at the httpservletrequest and httpservletresponse interfaces in the javax. servlet. http package.

The httpservletrequest interface inherits the servletrequest interface. Inherit the servletrequest interface to provide HTTP Servlet request information. The servlet container creates an httpservletrequest object and passes the httpservletrequest object as a parameter to the servlet service method (such as doget and dopost ).

The implementation class of the httpservletrequest interface is javax. servlet. http. httpservletrequestwrapper. The javax. servlet. http. httpservletrequestwrapper class also inherits the javax. servlet. servletrequestwrapper class. When developers want child classes to adapt to servlet requests, they can use the javax. servlet. http. httpservletrequestwrapper class to conveniently implement the httpservletrequest interface. The javax. servlet. http. httpservletrequestwrapper class implements the packaging mode and modifier mode.

Javax. servlet. http. httpservletrequestwrapper is similar to javax. servlet. servletrequestwrapper. They implement different interfaces: the former implements the httpservletrequest interface, and the latter implements the servletrequest interface.

As for the javax. servlet. http. httpservletrequestwrapper and javax. servlet. servletrequestwrapper classes, the httpservletrequest interface and servletrequest interface will be further studied.

I have a preliminary understanding of the httpservletrequest interface. Next I will take a look at the httpservletresponse interface.

Httpservletresponse interface, which inherits the servletresponse interface and provides HTTP-specific functions when sending a response. For example, some of its methods access HTTP headers and cookies. The implementation class of the httpservletresponse interface is httpservletresponsewrapper. To be continued

/*************************************** **************************************** *************************************/

Now, let's get to know the details of each method in the javax. servlet. http. httpservlet class.

 

1. Protected void doget (httpservletrequest req, httpservletresponse resp) throws servletexception, java. Io. ioexception
Parameter description:
REQ is an httpservletrequest object that contains a servlet composed of customer requests.
Resp is an httpservletresponse object that contains the response sent by the servlet to the customer.
The doget method is called by the server through the service method of the javax. servlet. http. httpservlet class, so that a servlet can process GET requests.
Override this method to support a GET request and automatically support an HTTP head request. A head request is a GET request response. Its return value has no body, but only the request header domain.
When this method is rewritten, the system reads the request data, writes the response header, obtains the write or output stream object of the response, and finally writes the response data. In this case, it is better to process contenttype and encoding. When a text output stream printing object is used to return a response, set the contenttype value before accessing the text output stream printing object.
Before a response is submitted, the servlet container must write the header because all headers must be sent before the HTTP transport response body section.
If possible, useServletResponse.setContentLength(int)Method To set the Content-Length (Content Length) value of the header, so that the servlet container can use a persistent connection to return its response to the customer, thus improving the performance. The length of the entire response content in the Response Buffer is automatically set.
The get method should be safe, that is, it is responsible for the controlled users without any side effects. For example, most forms of requests have no side effects. If the purpose of a client request is to change the storage data, the request should use some other HTTP methods.
The get method should also be idempotent, which means it can be safely repeated. Sometimes it makes a method safe and idempotent. For example, duplicate queries are both secure and idempotent, but it is neither secure nor idempotent to purchase or modify data for a product online.
If the request format is incorrect, the doget method returns an HTTP message with the content of "bad request.

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.