Httpservletresponse,httpservletrequest Detailed
1, the relevant interface
HttpServletRequest
The most common method for HttpServletRequest interfaces is to obtain the parameters in the request, which are typically data in the client form. At the same time, the HttpServletRequest interface can get the name delivered by the client, the server-side hostname and IP address that generated the request and receives the request, as well as information such as the communication protocol that the client is using. The following table is a common method for interface HttpServletRequest.
Description: The HttpServletRequest interface provides a number of methods.
Common methods of Interface HttpServletRequest
Square Method |
Speak clearly |
Getattributenames () |
Returns the name collection for all properties of the current request |
GetAttribute (String name) |
Returns the property value specified by name |
GetCookies () |
Returns the Cookie sent by the client |
GetSession () |
Returns the session associated with the client and returns null If no session is assigned to the client |
GetSession (Boolean Create) |
Returns the session associated with the client and , if no session is assigned to the client , creates a session and returns |
GetParameter (String name) |
Gets the parameter in the request, which is specified by name |
Getparametervalues (String name) |
Returns the value of the parameter in the request, which is specified by name |
Getcharacterencoding () |
Returns the character encoding of the request |
Getcontentlength () |
Returns the effective length of the request body |
getInputStream () |
Gets the data in the requested input stream |
GetMethod () |
Gets the way the request is sent, such as get,post |
Getparameternames () |
Gets the name of all parameters in the request |
Getprotocol () |
Gets the protocol name used by the request |
Getreader () |
Gets the data flow of the request body |
GETREMOTEADDR () |
Get the IP address of the client |
Getremotehost () |
Get the name of the client |
getServerName () |
Returns the name of the server that accepted the request |
Getserverpath () |
Gets the path of the requested file |
HttpServletResponse
In a servlet, the HttpServletResponse interface is used when the server responds to a request from the client. You can use the setContentType () method to set the type of response. Send character data, you can use Getwriter () to return an object. The following table is a common method for interface HttpServletResponse.
Common methods of Interface HttpServletResponse
Square Method |
Speak clearly |
Addcookie (Cookie cookie) |
Adds the specified cookie to the current response |
AddHeader (String name,string value) |
Adds the specified name and value to the header information of the response |
Containsheader (String name) |
Returns a Boolean value that determines whether the head of the response is set |
Encodeurl (String URL) |
Encode the specified URL |
Senderror (int SC) |
Sends an error to the client using the specified status code |
Sendredirect (String location) |
Send a temporary response to the client |
Setdateheader (String name,long date) |
Sets the given name and date to the head of the response |
SetHeader (String name,string value) |
Sets the given name and value to the head of the response |
SetStatus (int SC) |
Set the status code for the current response |
setContentType (String ContentType) |
Set the MIME type of the response |
2. Some different details
First, ServletRequest represents an HTTP request, which is an object in memory that is a container that can hold request parameters and properties. 1. When the request object is created, when a JSP or servlet is accessed through a URL, that is, when invoking the servlet's service (), DoPut (), DoPost (), Doxxx () method, The Web service server that executes the servlet automatically creates an object of ServletRequest and Servletresponse, passed to the service method as a parameter. 2. The request object is automatically generated by the servlet container, which automatically encapsulates the parameters of the Get and post submissions in the request, as well as the property values in the Request container, HTTP headers, and so on. When the servlet or JSP gets the request object, it knows where the request was sent, what resources were requested, what parameters to take, and so on. 3. Hierarchical structure of ServletRequestjavax.servlet.ServletRequest
javax.servlet.http.HttpServletRequest 4, through the Request object, you can obtain the session object and the client's cookie. 5, the request needs to specify the URL, the browser generates HTTP requests based on the URL and sent to the server, the requested URL has a certain specification:
Second, Servletresponse Is also created automatically by the container, which represents the response of the servlet to the client's request, and the content of the response is typically HTML, and HTML is only part of the response content. If another resource is included in the request, such as a picture on the page, a second HTTP request is used to get the picture content. the corresponding object has the following functions:1. Write a cookie to the client2. Rewrite URLs3. Get output stream object, write text or binary data to client4. Set the character encoding type of the response client browser5. Set the MIME type of the client browser. text: http://blog.csdn.net/yujin753/article/details/42239177
HttpServletResponse and HttpServletRequest detailed. RP