Detailed explanation of HttpServletRequest and HttpServletResponse

With HttpServletRequest, it is now organized as follows for future reference

There are many classes and interfaces related to requests and responses. The following table is the main classes and interfaces related to requests and interfaces.

Main classes and interfaces related to requests and interfaces

 

       method

illustrate

ServletInputStream

Servlet input stream

ServletOutputStream

Servlet output stream

ServletRequest

An interface representing a servlet request

ServletResponse

An interface representing a servlet response

ServletRequestWrapper

This class implements the ServletRequest interface

ServletResponseWrapper

This class implements the ServletResponse interface

HttpServletRequest

Inherited the ServletRequest interface, representing HTTP requests

HttpServletResponse

Inherited the ServletResponse interface, representing HTTP requests

HttpServletRequestWrapper

Implementation of HttpServletRequest

HttpServletResponseWrapper

Implementation of HttpServletResponse

Among the classes and interfaces given above, the most important ones are the HttpServletRequest and HttpServletResponse interfaces, which will be described in detail below.

1.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 generated the request and received the request, and can also obtain 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

 

2.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

Guess you like

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