JSP server response
Response is primarily in response to the result of the JSP container back to the client process. May be provided by the HTTP response status variables and transmitting data, such as cookies, HTTP header information to the client.
A typical response looks like this:
HTTP/1.1 200 OK Content-Type: text/html Header2: ... ... HeaderN: ... (空行) <!doctype ...> <html> <head>...</head> <body> ... </body> </html>
Contains status line HTTP version information, such as HTTP / 1.1, a status code, such as 200, there is a very short message corresponds to the status code, such as OK.
The following table summarizes the response header HTTP1.1 most useful part, you'll often see them in network programming:
Response header | description |
---|---|
Allow | Specify the server supports request methods (GET, POST, etc.) |
Cache-Control | Specifies the response document can be cached security situation. Value is typically public , Private , or no-cache and the like. Public means that documents can be cached, Private means that the document is only a single user service and only use the private cache. No-cache means that the document will not be cached. |
Connection | Command the browser whether to use persistent HTTP connections. close value command the browser does not use persistent HTTP connections, and keep-alive means using persistent connections. |
Content-Disposition | Let the browser requires the user to respond to a given name is stored on disk |
Content-Encoding | When specify transfer encoding rules page |
Content-Language | The expression language used in the document, such as en, en-us ,, ru etc. |
Content-Length | It indicates the number of bytes of the response. Only in the browser uses of persistent helpful if (keep-alive) HTTP connections |
Content-Type | MIME type of the document shows that the use of |
Expires | When are specified expired and removed from the cache |
Last-Modified | Indicating the document was last modified. The client can cache a document and subsequent requests in the If-Modified-Since request header |
Location | In 300 seconds, all containing a status code in response to the address, the browser will automatically retrieve the new document and then reconnect |
Refresh | Indicate how often the browser requests a page update. |
Retry-After | After use with a 503 (Service Unavailable) to tell the user how long the request will get a response |
Set-Cookie | Indicates the current page corresponding cookie |
HttpServletResponse类
javax.servlet.http.HttpServletResponse response object is an instance of class. Like server creates a request objects, it also creates a client response.
response object defines the process of creating an interface HTTP header information. By using this object, developers can add new cookie or time stamp, as well as HTTP status code, and so on.
The following table lists the HTTP response header to set methods, which are provided by HttpServletResponse categories:
S.N. | Methods & Description |
---|---|
1 | String encodeRedirectURL(String url) Of sendRedirect () URL encoding method |
2 | String encodeURL(String url) The URL encoding, return the URL contains a Session ID |
3 | boolean containsHeader(String name) Returns whether there is a response header |
4 | boolean isCommitted() Whether to return 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) Addition Response header values specified name and date |
7 | void addHeader(String name, String value) Addition Response header name and the specified value |
8 | void addIntHeader(String name, int value) Addition Response header and int specifying the name |
9 | void flushBuffer() The contents of any cache written to the client |
10 | void reset() Clears any data in any cache, including a status code and a variety of response headers |
11 | void resetBuffer() Substantially clear cached data, excluding headers and response status code |
12 | void sendError(int sc) Specified status code sends an error response to the client, and then clear the cache |
13 | void sendError(int sc, String msg) Specified status code and a message is sent to the client error response |
14 | void sendRedirect(String location) Using the specified URL to the client sends a temporary indirect response |
15 | void setBufferSize(int size) Buffer size set in response to body |
16 | void setCharacterEncoding(String charset) 指定响应的编码集(MIME字符集),例如UTF-8 |
17 | void setContentLength(int len) 指定HTTP servlets中响应的内容的长度,此方法用来设置 HTTP Content-Length 信息头 |
18 | void setContentType(String type) 设置响应的内容的类型,如果响应还未被提交的话 |
19 | void setDateHeader(String name, long date) 使用指定名称和值设置响应头的名称和内容 |
20 | void setHeader(String name, String value) 使用指定名称和值设置响应头的名称和内容 |
21 | void setIntHeader(String name, int value) 指定 int 类型的值到 name 标头 |
22 | void setLocale(Locale loc) 设置响应的语言环境,如果响应尚未被提交的话 |
23 | void setStatus(int sc) 设置响应的状态码 |
HTTP响应头程序示例
接下来的例子使用setIntHeader()方法和setRefreshHeader()方法来模拟一个数字时钟:
<%@ 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>菜鸟教程(runoob.com)</title> </head> <body> <h2>自动刷新实例</h2> <% // 设置每隔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"); %> </body> </html>
将以上代码保存为main.jsp,然后通过浏览器访问它。它将会每隔5秒显示一下系统当前时间。
我们可以看下以下 Gif 演示图:
您也可以自己动手修改以上代码,试试使用其他的方法,将能得到更深的体会。