HttpServletResponse和HttpServletRequest

1. Related interfaces

 

HttpServletRequest

The most commonly used method of the HttpServletRequest interface is to obtain the parameters in the request, which are generally the data in the client form. At the same time, the HttpServletRequest interface can obtain the name transmitted by the client, the host name and IP address of the server that generates the request and receives the request, and also obtains information such as the communication protocol being used by the client. The following table shows the common methods of the interface HttpServletRequest.

Description: The HttpServletRequest interface provides many methods.

Common methods of interface HttpServletRequest

method

illustrate

getAttributeNames()

Returns the set of names of all attributes 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, or null if no session is assigned to the client

getsession(boolean create)

Returns the session related to the client. If no session is assigned to the client, create a session and return

getParameter(String name)

Get the parameters in the request, which are specified by name

getParameterValues(String name)

Returns the parameter value in the request, the parameter value is specified by name

getCharacterEncoding()

Returns the requested character encoding

getContentLength()

Returns the effective length of the request body

getInputStream()

Get data from the requested input stream

getMethod()

Get the method of sending the request, such as get, post

getParameterNames()

Get the names of all parameters in the request

getProtocol()

Get the protocol name used by the request

getReader()

Get the data stream of the request body

getRemoteAddr()

Get the IP address of the client

getRemoteHost()

Get the client's name

getServerName()

Returns the name of the server accepting the request

getServerPath()

Get the path of the requested file

 

HttpServletResponse

In servlets, the HttpServletResponse interface is used when the server responds to a request from the client. To set the type of the response, use the setContentType() method. To send character data, you can use getWriter() to return an object. The following table shows the common methods of the interface HttpServletResponse.

Common methods of interface HttpServletResponse

       method

illustrate

addCookie(Cookie cookie)

Add the specified cookie to the current response

addHeader(String name,String value)

Add the specified name and value to the response headers

containsHeader(String name)

Returns a boolean value that determines whether the response header is set

encodeURL(String url)

Encode the specified URL

sendError(int sc)

Send an error to the client with the specified status code

sendRedirect(String location)

send a provisional response to the client

setDateHeader(String name,long date)

Set the given name and date to the header of the response

setHeader(String name,String value)

Set the given name and value to the header 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 differences

一、ServletRequest
 
Represents an HTTP request. The request is an object in memory. This object is a container that can store request parameters and attributes.
 
1. When the request object is created, when a JSP or servlet is accessed through the URL, that is, when the servlet's service(), doPut(), doPost(), doXxx() methods are called, the servlet's web is executed The server automatically creates a ServletRequest and ServletResponse object and passes it to the service method as a parameter.
 
2. The request object is automatically generated by the servlet container. This object automatically encapsulates the parameters submitted by the get and post methods in the request, as well as the attribute values ​​in the request container, as well as the http header and so on. When a servlet or JSP gets the request object, it knows where the request came from, what resources are requested, what parameters it takes, and so on.
 
3. Hierarchy of ServletRequest
javax.servlet.ServletRequest 
  javax.servlet.http.HttpServletRequest
 
4. Through the request object, the Session object and the client's Cookie can be obtained.
 
5. The request needs to specify a URL. The browser generates an HTTP request based on the URL and sends it to the server. The requested URL has certain specifications:
二、ServletResponse
 
It is also automatically created by the container and represents the response of the Servlet to the client request. The content of the response is generally HTML, and HTML is only a part of the content of the response. If the request also contains other resources, it will be obtained in turn. For example, if the page contains pictures, a second http request will be made to obtain the picture content.
The corresponding object has the following functions:
1. Write a cookie to the client
2. Rewrite the URL
3. Get the output stream object and write text or binary data to the client
4. Set the character encoding type of the response client browser
5. Set the MIME type of the client browser.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325357007&siteId=291194637