Detailed explanation of java HttpServletRequest and HttpServletResponse
Recently, due to the reencapsulation of CAS-related JAR packages, I want to try to achieve zero configuration, and in this process, I have made a lot of
Use HttpServletRequest, and now organize it as follows for future reference. (The form is copied from another place, and the layout is a bit a bit, please read it.)
There are many classes and interfaces related to request and response. The following table is the main classes and interfaces related to request and interface.
Main classes and interfaces related to requests and interfaces
method | illustrate |
ServletInputStream | Servlet input stream |
ServletOutputStream | Servlet output stream |
ServletRequest | An interface that represents a Servlet request |
ServletResponse | An interface representing the Servlet response |
ServletRequestWrapper | This class implements the ServletRequest interface |
ServletResponseWrapper | This class implements the ServletResponse interface |
HttpServletRequest | Inherited the ServletRequest interface, indicating HTTP request |
HttpServletResponse | Inherited the ServletResponse interface, indicating HTTP request |
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. These two interfaces will be introduced in detail below.
1. HttpServletRequest
The most common method of the HttpServletRequest interface is to obtain parameters in the request, which are generally data in the client form. At the same time, the HttpServletRequest interface can obtain the name transmitted by the client, it can also obtain the server host name and IP address that generates the request and receives the request, and it can also obtain information such as the communication protocol that the client is using. The following table is a common method for the interface HttpServletRequest.
Note: The HttpServletRequest interface provides many methods.
Common methods of interface HttpServletRequest
method | illustrate |
getAttributeNames() | Returns the name collection of all attributes currently requested |
getAttribute(String name) | Returns the attribute value specified by name |
getCookies() | Return to the cookies sent by the client |
getsession() | Returns the session related to the client. If the session is not assigned to the client, it returns null |
getsession(boolean create) | Returns the client-related session. If the client is not assigned a session, create a session and return it. |
getParameter(String name) | Gets the parameter in the request, which is specified by name |
getParameterValues(String name) | Returns the parameter value in the request, which is specified by name |
getCharacterEncoding() | Returns the requested character encoding method |
getContentLength() | Returns the valid length of the request body |
getInputStream() | Get data from the requested input stream |
getMethod() | Get the method of sending requests, such as get and 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 that accepted the request |
getServerPath() | Get the path to the requested file |
2. HttpServletResponse
In Servlet, when the server responds to a client's request, the HttpServletResponse interface must be used. To set the response type, you can use the setContentType() method. To send character data, you can use getWriter() to return an object. The following table is a common method for 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 header information |
containsHeader(String name) | Returns a boolean value to determine whether the response header is set |
encodeURL(String url) | Encode the specified URL |
sendError(int sc) | Send 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) | Set the header of the response to the given name and date |
setHeader(String name,String value) | Set the header of the response to the given name and value |
setStatus(int sc) | Set status code for the current response |
setContentType(String ContentType) | Set the MIME type of response |
Thank you for reading, I hope it can help you. Thank you for your support for this site!