English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Response response object mainly transmits the result processed by the JSP container back to the client. You can set the HTTP status and send data to the client, such as Cookie, HTTP header information, etc., through the response variable.
A typical response looks like the following:
HTTP/1.1 200 OK Content-Type: ... text/html Header2: ... ... HeaderN: ... (Empty line) <!doctype ...> <html> <head>.../<head> <body> ... </body> </html>
The status line contains HTTP version information, such as HTTP/1.1, a status code, such as200, and there is also a very short message corresponding to the status code, such as OK.
The following table summarizes HTTP1.1The most useful part of the response headers, which you will often see in network programming:
Response headers | Description |
---|---|
Allow | Specifies the request methods supported by the server (GET, POST, etc.) |
Cache-Control | Specifies the cases in which the response document can be safely cached. Typically, the value is public,private orno-cache etc. Public means the document can be cached, Private means the document is for a single user and can only use private cache. No-Cache means the document is not cached. |
Connection | Instructs the browser whether to use a persistent HTTP connection.closeValue Instructs the browser not to use a persistent HTTP connection, but keep-Keep-alive means using a persistent connection. |
Content-Disposition | Instructs the browser to store the response with the given name on the disk |
Content-Encoding | Specifies the encoding rules for the page during transmission |
Content-Language | Expresses the language used by the document, such as en, en-us, ru, etc. |
Content-Length | Indicates the number of bytes of the response. It is only useful when the browser uses a persistent (keep--It is useful only when there is 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 modification time of the document. The client can cache the document and provide it in subsequent requests. If-Modified-SinceRequest header |
Location | In3Within 00 seconds, the browser will automatically reconnect and retrieve the new document if all the response addresses have a status code |
Refresh | Indicate how often the browser requests to update the page. |
Retry-After | With503 Used together with (Service Unavailable) to inform the user how long the request will take to receive a response |
Set-Cookie | Indicate the cookie corresponding to the current page |
The response object is an example of javax.servlet.http.HttpServletResponse. Just as the server creates a request object, it also creates a client response.
The response object defines the interface for handling the creation of HTTP information headers. By using this object, developers can add new cookies or timestamps, as well as HTTP status codes, etc.
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) Encode the URL used by the sendRedirect() method |
2 | String encodeURL(String url) Encode the URL and return the URL containing the Session ID |
3 | boolean containsHeader(String name) Return whether the specified response header exists |
4 | boolean isCommitted() Return whether the response has already been committed to the client |
5 | void addCookie(Cookie cookie) Add the specified cookie to the response |
6 | void addDateHeader(String name, long date) Add a response header with a specified name and date value |
7 | void addHeader(String name, String value) Add a response header with a specified name and value |
8 | void addIntHeader(String name, int value) Add a response header with a specified name and int value |
9 | void flushBuffer() Write any content in the cache to the client |
10 | void reset() Clear any data in any cache, including status codes and various response headers |
11 | void resetBuffer() Clear basic cache data, excluding response headers and status codes |
12 | void sendError(int sc) Send an error response to the client using the specified status code, then clear the cache |
13 | void sendError(int sc, String msg) Send an error response to the client using the specified status code and message |
14 | void sendRedirect(String location) Send a temporary indirect response to the client using the specified URL |
15 | void setBufferSize(int size) Set the buffer size of the response body |
16 | void setCharacterEncoding(String charset) Specify the response character encoding (MIME character set), such as UTF-8 |
17 | void setContentLength(int len) Specify the length of the response content in HTTP servlets, this method is used to set HTTP Content-Length information header |
18 | void setContentType(String type) Set the response content type, if the response has not been committed |
19 | void setDateHeader(String name, long date) Set the name and date of the response header using the specified name and date |
20 | void setHeader(String name, String value) Set the name and content of the response header using the specified name and value |
21 | void setIntHeader(String name, int value) Specify an int type value to the name header |
22 | void setLocale(Locale loc) Set the response locale, if the response has not been committed |
23 | void setStatus(int sc) Set the response status code |
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.*" %> !DOCTYPE html <html> <head> <meta charset="utf-8"> <title>Basic Tutorial Website(oldtoolbag.com)</title>/<title> </<head> <body> <h2>Auto Refresh Example</h2> <% // Set every5seconds auto refresh response.setIntHeader("Refresh", 5); // Get 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: " + CT + "\n"); %> </body> </html>
Save the above code as main.jsp and then access it through a browser. It will refresh every5The second will display the current system time.
You can also modify the code above by yourself and try using other methods to gain a deeper understanding.