The response response object mainly passes the result of the JSP container processing back to the client. You can set the state of HTTP through the response variable and send data to the client, such as cookies, HTTP file header information, and so on.
A typical response looks like this:
http/1.1 okcontent-type:text/htmlheader2: ... HeaderN: ... (Blank line) <!doctype ... >
The status line contains HTTP version information, such as http/1.1, a status code such as 200, and a very short message that corresponds to a status code, such as OK.
The following table summarizes the most useful parts of the HTTP1.1 response header, which you will see frequently in network programming:
Response Header |
Description |
Allow |
Specify the request method supported by the server (Get,post, etc.) |
Cache-control |
Specifies the scenario in which the response document can be safely cached. Usually the values are public,private or No-cache , and so on. Public means that the document is cacheable, and private means that the document is only for single-user service and only uses private caches. No-cache means that the document is not cached. |
Connection |
Command whether the browser will use a persistent HTTP connection. Close The value command Browser does not use persistent HTTP connections, while keep-alive means using persistent connections. |
Content-disposition |
Let the browser require the user to store the response in a given name on disk |
Content-encoding |
Specify encoding rules for pages at transfer time |
Content-language |
Language used to describe the document, such as EN, en-us,,ru, etc. |
Content-length |
Indicates the number of bytes in the response. Only useful if the browser is using persistent (keep-alive) HTTP connections |
Content-type |
Indicates the MIME type used by the document |
Expires |
Specify when to expire and remove from the cache |
Last-modified |
Indicates when the document was last modified. The client can cache the document and provide a if-modified-since request header in subsequent requests |
Location |
Within 300 seconds, the browser will automatically re-connect and retrieve the new document with all the response addresses with a status code |
Refresh |
Indicates how often the browser requests the page to be updated. |
Retry-after |
Used with 503 (Service unavailable) to tell the user how long the request will be responded to |
Set-cookie |
Indicates the cookie that corresponds to the current page |
HttpServletResponse class
The response object is an instance of the Javax.servlet.http.HttpServletRequest class. Just as the server creates a Request object, it also creates a client response.
The response object defines the interface that handles the creation of HTTP information headers. By using this object, developers can add new cookies or timestamps, HTTP status, 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) Encode the URL used by the Sendredirect () method |
2 |
String encodeurl (string url) URL encoding, callback URL containing 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) Add the specified cookie to the response |
6 |
void Adddateheader (String name, long date) Add a response header and date value for the specified name |
7 |
void AddHeader (string name, String value) Add a response header and value for the specified name |
8 |
void Addintheader (String name, int value) Adds a response header and int value for the specified name |
9 |
void Flushbuffer () Write content from 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) Set the buffer size of the response body |
16 |
void Setcharacterencoding (String charset) Specifies the encoding set of the response (MIME character set), such as UTF-8 |
17 |
void setcontentlength (int len) Specifies the length of the contents of the response in HTTP Servlets, which is used to set the HTTP content-length information header |
18 |
void setContentType (String type) Sets the type of the content of the response, if the response has not yet been committed |
19 |
void Setdateheader (String name, long date) Sets the name and content of the response header with the specified name and value |
20 |
void SetHeader (string name, String value) Sets the name and content of the response header with the specified name and value |
21st |
void Setintheader (String name, int value) Sets the name and content of the response header with the specified name and value |
22 |
void SetLocale (Locale Loc) Set the locale of the response, if the response has not yet been committed |
23 |
void SetStatus (int sc) Set the status code of the response |
HTTP Response Header Program example
The next 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 to automatically refresh Response.setintheader every 5 seconds ("Refresh", 5); //get current time calendar calendar= NewGregorianCalendar (); Stringam_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"; Elseam_pm= "PM"; StringCT= Hour+":"+ minute +":"+ Second +" "+am_pm; Out.println ("Current time is :" +CT+ "\ n");%></Center></Body></HTML>
Save the above code as main.jsp and then access it through your browser. It will display the current time of the system every 5 seconds.
The results of the operation are as follows:
Auto Refresh Header examplecurrent time is:9:44:50 PM
You can also modify the above code yourself, try to use other methods, you will be able to get a deeper understanding.
JSP Server Response