Javaee--httpservletrequest Object

Source: Internet
Author: User
Tags html code example

Statement: The material used in this column is written by the VIP students of the Kay Academy, who has the right to be anonymous and has the final right to interpret the article; The Edith Academy is designed to promote students to learn from each other on the basis of public notes.

HttpServletRequest Object

We all know that when the browser communicates with Tomcat (Web server) over the HTTP protocol, it generates two objects, one HttpServletRequest object and one HttpServletResponse object. They are a pair of data-encapsulated objects that encapsulate the client's request header, which encapsulates the server's response header. And here to introduce is the HttpServletRequest object, HttpServletRequest is actually an interface, is a Java custom interface, this interface is developed by the people of the Web server to do the implementation.
Can look at the source code of HttpServletRequest:

We can look at the two classes in Tomcat that implement this interface:
Request class:

Requestfacade class:

You can see that these two classes each implement the HttpServletRequest interface, but actually the two classes are related, the specific code implementation is actually done by the request class, and the Requestfacade class just as a forwarding exists. And from the name of the class can also be seen, facade is the appearance, the positive meaning, so this is an appearance class, and in this Requestfacade class is the real implementation class is request. Can be imagined as Requestfacade is the front end of the request, the façade, request is requestfacade backstage, backend.
Take a look at Requestfacade's constructor and part of the code to know:

Let's take a look at some of Requestfacade's code:

And this class has less than 1000 lines of code:

Let's look at the code for the request class below:

You can see that the Getcontextpath method is implemented on the request class, and because the implementation code is in this class, the number of lines of code is more than the Requestfacade class:

A pattern is formed between the HttpServletRequest interface and the request, the Requestfacade implementation class, which is the appearance pattern. The appearance mode provides a consistent interface for a set of interfaces in a subsystem, which is Requestfacade and defines a high-level interface that is httpservletrequest, which makes the subsystem easier to use.
The advantage of the appearance pattern is that it hides the complexity of the system and provides the client with an interface (HttpServletRequest) to which the client can access the system. This type of design pattern is a structural pattern, which adds an interface to an existing system to hide the complexity of the system, which realizes the loosely coupled relationship between the subsystem and the client. So we all call the method through the HttpServletRequest interface object, and the implementation class and interface classes are hidden behind, and these two classes are not implemented in Java, but are implemented by the people who develop the Web server (the urine of Java has always been so).

About Object pools:
The HttpServletRequest and HttpServletResponse objects are stored in an object pool, which is an active, self-growing pool of objects that, like the auto-growing thread pool, adds new objects whenever the object is not enough. This object pool optimizes server resources because these two objects can be reused over and over again without wasting resources. Without this object pool, every time a user accesses a new object, it is very resource-intensive and slow. So from this we know that this object pool is the role of an optimized resource (as is the case in the pool).
We can print the hash value of the two objects to see what we know (need to constantly refresh the access page):

Operation Result:

You can see how many hash values have been repeated several times, so you can tell from this that it uses the object pooling mechanism.

Well, the above simple introduction of httpservletrequest in the interface and implementation of some of the relationship and the use of what design patterns and object pool mechanism, next introduce some of the more commonly used methods in HttpServletRequest:
Methods for obtaining server-related information:

code example:

Operation Result:

Get the Request header information method (the information in the request header is in the form of a key/value pair):

code example:

Operation Result:

Obtain the IP and port methods of the client:

code example:

Operation Result:

Gets and sets the form data method (the data in the file cannot be obtained if the file is uploaded):

HTML code example:

Java code example:

Browser form:

Console results:

Get and set form properties related methods:

Note: These methods are only within the Web container flow, only in the forwarding relationship between the Web Components are shared, it is only valid in this scope, so you can not directly get the value into the code to print, the following use the actual example demonstrates that you can not directly get the value:

code example:

Browser:

Console results:

Javaee--httpservletrequest Object

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.