Java Web Foundation Summary five--httpservletrequest and HttpServletResponse
As previously summarized, each time the client sends an HTTP request to the Web server, the Web server creates a request object for each request, and a response object representing the response, respectively. The request and response objects represent requests and responses, so we can get request-related data and operations through the requests object. The response-related data encapsulation and some other operations are performed through the response object.
A. HttpServletRequest and HttpServletResponse implementation class
We will find that both HttpServletRequest and httpservletresponse are interfaces, so what are the implementation classes when they run? Who is responsible for instantiating their implementation classes?
The answer can be obtained by debugging the Helloworldservlet of the previous article. Part of the code for Helloworldservlet is as follows:
This picture is obtained during debugging:
As we can see from the above figure, the actual type of HttpServletRequest and HttpServletResponse object REQ,RESP is Requestfacade and Responsefacade. These two classes are all under the Org.apache.catalina.connector package. That's the two classes I used for the Web server tomcat. Therefore, the Web container is responsible for instantiating HttpServletRequest and HttpServletResponse objects.
two. HttpServletRequest Introduction
The HttpServletRequest object represents the client's request, and when the client accesses the server over the HTTP protocol, all the information in the HTTP request header is encapsulated in the object.
The methods and actions commonly used by the request:
1. Access to client information
The Getrequesturl method returns the full URL of the client when the request is made.
The Getrequesturi method returns the part of the resource name in the request row, removing the host name.
The Getremoteaddr method returns the IP address of the client that made the request
The Getremotehost method returns the full host name of the client that made the request
The Getremoteport method returns the port number used by the client
The Getlocaladdr method returns the IP address of the Web server.
Getlocalname method returns the host name of the Web server
GetMethod Gets the client request method, such as Get,post
2. Some methods of obtaining the request header
GetHead (name) method
Getheaders (String name) method
Getheadernames method
3. Get the request parameters, that is, some methods of the data submitted by the client.
GetParameter (name) method
Getparametervalues (String name) method
Getparameternames method
Getparametermap method
4.HttpServletRequest Implementation Forwarding
Request forwarding refers to a Web resource that receives a client request and notifies the server to invoke another Web resource for processing. The Request object provides a Getrequestdispatcher method that returns a RequestDispatcher object that calls the forward method of the object to enable request forwarding.
5. Request Domain
The request object is also a domain object, and we can use the request object to bring the data through the request object to other Web resources as we implement the forwarding process. Here are some common ways to manipulate properties in a domain:
SetAttribute method
GetAttribute method
RemoveAttribute method
Getattributenames method
6. The difference between the GetParameter and GetAttribute methods of the request.
Since request is also a domain object, you can get parameters from it, that is, parameter. You can also get the properties in the domain. But the meaning of them is totally different.
GetParameter (String name) gets the parameter value that the client sends to the server, which is specified by name and is typically a parameter in the form. and the parameter can only be a key-value pair in string form.
GetAttribute (String name): Returns the property value specified by name and returns a null value if the specified property value does not exist. Here is also a key-value pair, the difference is that the value here can be any type.
three. HttpServletResponse Introduction
The HttpServletResponse is the response object to the server. This object encapsulates the method of sending data to the client, sending a response header, and sending a response status code.
Response commonly used methods and operations:
1. Common Methods
Addcookie (Cookie cookie) writes cookies to the client
AddHeader (java.lang.String name, java.lang.String value) writes the given response header
Encodeurl (Java.lang.Stringurl) The default cookie contains session ID, if the client does not support cookies, in the parameter URL to add session ID information, you can resolve the user to disable the cookie problem.
SetStatus (INTSC) sets the status code of the response.
2. Differences between the Getoutputstream and Getwriter methods
The Getoutputstream and Getwriter methods are used to obtain the output binary data, output text data servletouputstream, PrintWriter objects respectively. The two methods of Getoutputstream and getwriter are mutually exclusive, and any one of them is called, and no other method can be called.
The data written by these two methods will be output to the client as the body of the response message, combined with the response status line and each response header. After the Serlvet service method finishes, the Web container checks whether the output stream object returned by the Getwriter or Getoutputstream method has called the Close method, and if not, the Web container calls the Close method to close the output stream object.
3. HttpServletResponse for redirection
Redirection refers to a Web resource receiving a client request, and the Web server notifies the client to access another Web resource, which is called request redirection. The implementation method is to call the Response.sendredirect () method. The principle of implementation is to return the 302 status code and location header to the client.
Four. The difference between forwarding forward and redirecting redirect
Forwarding is implemented on the server side. Once a Web resource receives a client request, it notifies the server to call another Web resource for processing, called request forwarding. The browser address bar keeps the initial URL address intact after the request forwarding process that calls the Requestdispatcher.forward method ends.
Redirection is implemented on the client side. Once a Web resource receives a client request, it notifies the client's browser to access another Web resource, called a request redirection. So the URL that is displayed in the browser address bar changes when the access process that calls the Httpservletresponse.sendredirect method redirects, and the initial URL address becomes the destination URL of the redirect.
The Requestdispatcher.forward method can only forward requests to other resources in the same web app; The Sendredirect method also redirects resources to other applications in the same site, even to other sites using an absolute URL.
The Httpservletresponse.sendredirect method responds directly to the request of the browser, and the result of the response is to tell the browser to re-issue the access request to another URL, and the Requestdispatcher.forward method forwards the request to the other one on the server side. Resources, rather than being visible to the client.
The caller of the Requestdispatcher.forward method shares the same request object and the response object as the callee, which belong to the same access request and response process, while the Httpservletresponse.sendredirect method caller and the The caller uses the respective request object and the response object, which belong to two separate access request and response procedures. In other words, the redirect generates a new request object and a response object.
Java Web Foundation Summary five--httpservletrequest and HttpServletResponse