YouTip LogoYouTip

Jsp Server Response

The Response object primarily transmits 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, HTTP header information, etc. A typical response looks like this: HTTP/1.1 200 OK Content-Type: text/html Header2: ......HeaderN: ... (empty line)...... 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 the HTTP 1.1 response headers, which you will frequently encounter in network programming: | **Response Header** | **Description** | | --- | --- | | Allow | Specifies the request methods supported by the server (GET, POST, etc.) | | Cache-Control | Specifies the conditions under which the response document can be safely cached. Common values are **public**, **private**, or **no-cache**. Public means the document can be cached, Private means the document is for a single user and can only use a private cache. No-cache means the document is not cached. | | Connection | Instructs the browser whether to use a persistent HTTP connection. The **close** value instructs the browser not to use a persistent HTTP connection, while keep-alive means to use 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 scheme for the page during transmission | | Content-Language | Indicates the language used by the document, such as en, en-us, ru, etc. | | 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 | Specifies 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 an **If-Modified-Since** request header in subsequent requests | | Location | Contains the address of the response with a status code in the 300 range; the browser will automatically reconnect and retrieve the new document | | Refresh | Specifies how often the browser should request an updated page. | | Retry-After | Used with 503 (Service Unavailable) to tell the user how long until the request will be responded to | | Set-Cookie | Specifies the cookie corresponding to the current page | * * * ## HttpServletResponse Class The response object is an instance of the javax.servlet.http.HttpServletResponse class. Just as the server creates a 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, etc. The following table lists the methods provided by the HttpServletResponse class for setting HTTP response headers: | **S.N.** | **Method & Description** | | --- | --- | | 1 | **String encodeRedirectURL(String url)** Encodes the URL for use with the sendRedirect() method | | 2 | **String encodeURL(String url)** Encodes the URL, returning a URL that includes the Session ID | | 3 | **boolean containsHeader(String name)** Returns whether the specified response header exists | | 4 | **boolean isCommitted()** Returns whether the response has already 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 buffered content to the client | | 10 | **void reset()** Clears any buffered data, including the status code and various response headers | | 11 | **void resetBuffer()** Clears the basic buffered 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 redirect response to the client using the specified URL | | 15 | **void setBufferSize(int size)** Sets the size of the buffer for the response body | | 16 | **void setCharacterEncoding(String charset)** Specifies the character encoding (MIME charset) for the response, such as UTF-8 | | 17 | **void setContentLength(int len)** Specifies the length of the content in an HTTP servlet response; this method sets 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 a response header with the specified name and date | | 20 | **void setHeader(String name, String value)** Sets a response header with the specified name and value | | 21 | **void setIntHeader(String name, int value)** Sets an int-typed value for the specified header name | | 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 for the response | * * * ## HTTP Response Header Program Example The following example uses the setIntHeader() method and the setRefreshHeader() method to simulate a digital clock:

Auto Refresh Example

Save the above code as main.jsp, then access it via a browser. It will display the current system time every 5 seconds. We can see the following Gif demonstration: !(#) You can also try modifying the above code yourself and experiment with other methods to gain a deeper understanding.
← Jsp Http Status CodesJsp Client Request β†’