JSP Server Response
The Response object mainly sends the results processed by the JSP container back to the client. You can use the response variable to set the HTTP status and send data to the client, such as cookies and HTTP header information.
A typical response looks like this:
HTTP/1.1 200 OK
Content-Type: text/html
Header2: ...
...
HeaderN: ...
(空行)
...
...
The status line contains the HTTP version, 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 encounter in network programming:
| Response headers | Description |
|---|---|
| Allow | Specifies the request methods supported by the server (GET, POST, and so on) |
| Cache-Control | Specifies the conditions under which the response document can be safely cached. The usual values are public**,private orno-cache**, and so on. Public means the document can be cached; Private means the document is intended for a single user and can only be stored in a private cache. No-cache means the document is not cached. |
| Connection | Instructs the browser whether to use a persistent HTTP connection.close**** value Instructs the browser not to use a persistent HTTP connection, while keep-alive means using a persistent connection. |
| Content-Disposition | Asks the browser to prompt the user to store the response on disk with a given name |
| Content-Encoding | Specifies the encoding rules of the page during transmission |
| Content-Language | Describes the language used by the document, such as en, en-us, ru, and so on |
| Content-Length | Indicates the number of bytes in the response. Only useful when the browser uses a persistent (keep-alive) HTTP connection |
| Content-Type | Indicates the MIME type used by the document |
| Expires | Indicates when the document expires and should be removed from the cache |
| Last-Modified | Indicates the last modified time of the document. The client can cache the document and provide a If-Modified-SinceRequest headers |
| Location | Within 300 seconds, for responses containing a status code, the browser automatically reconnects and retrieves a new document |
| Refresh | Indicates how often the browser should request a page refresh. |
| Retry-After | Used together with 503 (Service Unavailable) to tell the user how long before the request will be responded to |
| Set-Cookie | Indicates the cookies associated with the current page |
The 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 an interface for handling the creation of HTTP headers. By using this object, developers can add new cookies or timestamps, as well as HTTP status codes, and so on.
The following table lists the methods used to set HTTP response headers, which are provided by the HttpServletResponse class:
| S.N. | Method** & **Description |
|---|---|
| 1 | String encodeRedirectURL(String url) Encodes the URL used by the sendRedirect() method |
| 2 | String encodeURL(String url) Encodes the URL, returning a URL that contains the Session ID |
| 3 | boolean containsHeader(String name) Returns whether the specified response header exists |
| 4 | boolean isCommitted() Returns whether the response has been committed to the client |
| 5 | void addCookie(Cookie cookie) Adds the specified cookie to the response |
| 6 | void addDateHeader(String name, long date) Adds a response header with the specified name and date value |
| 7 | void addHeader(String name, String value) Adds a response header with the specified name and value |
| 8 | void addIntHeader(String name, int value) Adds a response header with the specified name and int value |
| 9 | void flushBuffer() Writes any content in the buffer to the client |
| 10 | void reset() Clears any data in the buffer, including the status code and all response headers |
| 11 | void resetBuffer() Clears the basic buffer data, excluding response headers and the status code |
| 12 | void sendError(int sc) Sends an error response to the client using the specified status code, then clears the buffer |
| 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 character encoding of the response (MIME character set), for example, UTF-8 |
| 17 | void setContentLength(int len) Specifies the length of the content of the response in HTTP servlets; this method is used to set the HTTP Content-Length header |
| 18 | void setContentType(String type) Sets the content type of the response, if the response has not yet been committed |
| 19 | void setDateHeader(String name, long date) Sets the name and date of a response header using the specified name and date |
| 20 | void setHeader(String name, String value) Sets the name and content of a response header using the specified name and value |
| 21 | void setIntHeader(String name, int value) Assigns an int value to the name header |
| 22 | void setLocale(Locale loc) Sets the locale of the response, if the response has not yet been committed |
| 23 | void setStatus(int sc) Sets the status code of the response |
HTTP Response Header Program Example
The following example uses the setIntHeader() method and the setRefreshHeader() method to simulate a digital clock:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
Runoob Tutorial(runoob.com)
自动刷新实例
<%
// 设置每隔5秒自动刷新
response.setIntHeader("Refresh", 5);
// 获取当前时间
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("当前时间: " + CT + "\n");
%>
Save the above code as main.jsp and access it through a browser. It will display the current system time every 5 seconds.
We can look at the following GIF demonstration:

You can also modify the above code yourself and try using other methods to gain a deeper understanding.