JSP server response
The Response object mainly transmits the results processed by the JSP container back to the client. You can set the HTTP status and send data to the client through the response variable, such as cookies, HTTP file header information, etc.
A typical response looks like the following:
HTTP/1.1 200 OK Content-Type: text/html Header2: ... ... HeaderN: ... (空行) <!doctype ...> <html> <head>...</head> <body> ... </body> </html>
The status line contains HTTP version information, such as HTTP/1.1, a status code, such as 200, and a very short message Corresponds to the status code, such as OK.
The following table summarizes the most useful parts of the HTTP1.1 response headers, which you will often see in network programming:
Response header | Description |
---|---|
Specify the request methods supported by the server (GET, POST, etc.) | |
Specifies the circumstances under which response documents can be safely cached. Usually the value is | public, private or no-cache etc. Public means that the document is cacheable, and Private means that the document is only served to a single user and can only use private cache. No-cache means the document is not cached. |
Instructs the browser whether to use persistent HTTP connections. | closeValue instructs the browser not to use persistent HTTP connections, while keep-alive means to use persistent connections. |
Let the browser ask the user to store the response on disk under the given name | |
Specify the encoding rules of the page during transmission | |
The language used to express the document, such as en, en-us,, ru, etc. | |
Indicates the number of bytes in the response. Only useful if the browser uses a persistent (keep-alive) HTTP connection | |
Indicates the MIME type used by the document | |
Specify when to expire and remove from cache | |
Indicates when the document was last modified.The client can cache the document and provide an If-Modified-Sincerequest header | |
in subsequent requests. Location | Within 300 seconds, including all response addresses with a status code, the browser will automatically reconnect and retrieve new documents |
Refresh | Indicates how often the browser requests updates to the page. |
Retry-After | Used with 503 (Service Unavailable) to tell the user how long it will take for the request to be responded to |
Set-Cookie | Indicate the cookie corresponding to the current page |
HttpServletResponse Class
The response object is an instance of the javax.servlet.http.HttpServletResponse class. Just as the server creates the request object, it also creates a client response.
The response object defines the interface for processing the creation of HTTP information headers. By using this object, developers can add new cookies or timestamps, HTTP status codes, and more.
The following table lists the methods used to set HTTP response headers. These methods are provided by the HttpServletResponse class:
##S.N. | Method & Description |
---|---|
String encodeRedirectURL(String url) |
Encode the URL used by the sendRedirect() method |
String encodeURL(String url) |
Encode the URL and return the URL containing the Session ID |
boolean containsHeader(String name) |
Returns whether the specified response header exists |
boolean isCommitted() |
Return whether the response has been submitted to the client |
void addCookie(Cookie cookie) |
Add the specified cookie to the response |
void addDateHeader(String name, long date) |
Add the response header and date value of the specified name |
void addHeader(String name, String value) |
Add the response header and value of the specified name |
void addIntHeader(String name, int value) |
Add the response header and int value of the specified name |
void flushBuffer() |
Write any cached content to the client |
void reset() |
Clear any data in any cache, including status codes and various response headers |
void resetBuffer() | Clear basic cache data, excluding response headers and status codes |
12 | void sendError(int sc) |
13 | void sendError(int sc, String msg) |
14 | void sendRedirect(String location) |
15 | void setBufferSize(int size) |
16 | void setCharacterEncoding(String charset) |
17 | void setContentLength(int len) |
18 | void setContentType(String type) |
19 | void setDateHeader(String name, long date) |
20 | void setHeader(String name, String value)
|
void setIntHeader(String name, int value)
| |
void setLocale(Locale loc)
| |
void setStatus(int sc)
|