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 this:
HTTP/1.1 200 OKContent-Type: text/htmlHeader2: ...HeaderN: ... (Blank Line)<!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 corresponding to the status code, such as OK.
The following table summarizes the most useful parts of HTTP 1.1 response headers, which you will often see in network programming:
response header | describe |
---|---|
Allow | Specify the request methods supported by the server (GET, POST, etc.) |
Cache-Control | 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. |
Connection | Instructs the browser whether to use persistent HTTP connections. The close value instructs the browser not to use persistent HTTP connections, while keep-alive means to use persistent connections. |
Content-Disposition | Let the browser ask the user to store the response on disk under the given name |
Content-Encoding | Specify the encoding rules for the page during transmission |
Content-Language | The language used to express the document, such as en, en-us, ru, etc. |
Content-Length | Indicates the number of bytes in the response. Only useful if the browser uses keep-alive HTTP connections |
Content-Type | Indicates the MIME type used by the document |
Expires | Indicate when to expire and remove from cache |
Last-Modified | Indicates when the document was last modified. The client can cache the document and provide an If-Modified-Since request 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 before the request is responded to |
Set-Cookie | Indicate the cookie corresponding to the current page |
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 handling the creation of HTTP 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:
SN | Method & Description |
---|---|
1 | String encodeRedirectURL(String url) encodes the URL used by the sendRedirect() method |
2 | String encodeURL(String url) encodes the URL and returns the URL containing the Session ID. |
3 | boolean containsHeader(String name) returns whether the specified response header exists |
4 | boolean isCommitted() returns whether the response has been submitted to the client |
5 | void addCookie(Cookie cookie) adds the specified cookie to the response |
6 | void addDateHeader(String name, long date) adds the response header and date value of the specified name |
7 | void addHeader(String name, String value) adds the response header and value of the specified name |
8 | void addIntHeader(String name, int value) adds the response header and int value of the specified name |
9 | void flushBuffer() writes the contents of any cache to the client |
10 | void reset() clears any data in any cache, including status codes and various response headers |
11 | void resetBuffer() clears basic cache data, excluding response headers and status codes |
12 | void sendError(int sc) sends an error response to the client using the specified status code, and then clears the cache |
13 | void sendError(int sc, String msg) sends an error response to the client using the specified status code and message |
14 | void sendRedirect(String location) sends a temporary indirect response to the client using the specified URL |
15 | void setBufferSize(int size) sets the buffer size of the response body |
16 | void setCharacterEncoding(String charset) specifies the response encoding set (MIME character set), such as UTF-8 |
17 | void setContentLength(int len) specifies the length of the response content in HTTP servlets. This method is used to set the HTTP Content-Length information header. |
18 | void setContentType(String type) sets the content type of the response, if the response has not been submitted yet |
19 | void setDateHeader(String name, long date) sets the name and content of the response header using the specified name and value |
20 | void setHeader(String name, String value) sets the name and content of the response header using the specified name and value |
twenty one | void setIntHeader(String name, int value) sets the name and content of the response header using the specified name and value |
twenty two | void setLocale(Locale loc) sets the locale of the response, if the response has not been submitted yet |
twenty three | void setStatus(int sc) sets the status code of the response |
The following example uses the setIntHeader() method and the setRefreshHeader() method to simulate a digital clock:
<%@ page import="java.io.*,java.util.*" %><html><head><title>Auto Refresh Header Example</title></head><body><center><h2 >Auto Refresh Header Example</h2><% //Set automatic refresh every 5 seconds response.setIntHeader("Refresh", 5); //Get the current time Calendar calendar = new GregorianCalendar(); String am_pm; int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); if(calendar.get(Calendar.AM_PM) == 0) am_pm = "AM"; else am_pm = "PM"; String CT = hour+":"+ minute +":"+ second +" "+ am_pm; out.println("Current Time is: " + CT + "n"); %></center></body></html>
Save the above code as main.jsp and access it through your browser. It will display the current system time every 5 seconds.
The running results are as follows:
Auto Refresh Header ExampleCurrent Time is: 9:44:50 PM
You can also modify the above code yourself and try other methods to gain a deeper understanding.