WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSP server response


Response response object mainly passes the result of the JSP container processing back to the client. You can set the HTTP status and send data to the client through the response variable, such as cookies, HTTP header information, and so on.

A typical response looks like this:

HTTP/1.1 200 OK
Content-Type: text/html
Header2: ...
...
HeaderN: ...
  (Blank line)
<!doctype ...>
<html>
<head>...</head>
<body>
...
</body>
</html>

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 see frequently in network programming:

Response header Description
Allow Specify the request method supported by the server (GET, POST, etc.)
Cache-Control Specifies the case where the response document can be cached securely. Usually the value is publicprivate orno-cache and many more. Public means that the document is cacheable, and Private means that the document is only served to a single user and can only use a private cache. No-cache means that the document is not cached.
Connection Whether the command browser wants to use a persistent HTTP connection. closevalue The command browser does not use persistent HTTP connections, and keep-alive means using persistent connections.
Content-Disposition Let the browser ask the user to store the response on disk with the given name
Content-Encoding Specify the encoding rules for the page when transmitting
Content-Language Express the language used by the document, such as en, en-us, ru, etc.
Content-Length Indicates the number of bytes of the response. Only useful if the browser uses a keep-alive HTTP connection
Content-Type Indicates the MIME type used by the document
Expires Indicates that the timeout expires and is removed from the cache
Last-Modified Indicate when the document was last modified. The client can cache the document and provide one in subsequent requests. If-Modified-SinceRequest header
Location Within 300 seconds, including all response addresses with a status code, the browser will automatically reconnect and retrieve the new document
Refresh Indicates how often the browser requests to update the page.
Retry-After Used in conjunction with 503 (Service Unavailable) to tell the user how long the request will get a response
Set-Cookie Indicate 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 the interface that handles the creation of HTTP headers. By using this object, developers can add new cookies or timestamps, as well as HTTP status codes and more.

The following table lists the methods used to set the HTTP response headers provided by the HttpServletResponse class:

S.N. Methods & 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)

Returns whether the specified response header exists
4 boolean isCommitted()

Return whether the response has been submitted 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)

Add response header and int value for the specified name
9 void flushBuffer()

Write any cached content to the client
10 void reset()

Clear any data in any cache, including status codes and various response headers
11 void resetBuffer()

Clear basic cached data, excluding response headers and status codes
12 void sendError(int sc)

Send an error response to the client with 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)

Specifies the encoding set (MIME character set) of the response, such as UTF-8
17 void setContentLength(int len)

Specifies the length of the content of the response in the HTTP servlets. This method is used to set the HTTP Content-Length header
18 void setContentType(String type)

Set the type of content of the response, if the response has not yet been submitted
19 void setDateHeader(String name, long date)

Set the name and content of the response header with the specified name and value
20 void setHeader(String name, String value)

Set the name and content of the response header with the specified name and value
21 void setIntHeader(String name, int value)

Specify a value of type int to the name header
22 void setLocale(Locale loc)

Set the locale of the response, if the response has not yet been submitted
23 void setStatus(int sc)

Set the response status code

HTTP response header example

Response header Description
Allow Specify the request method supported by the server (GET, POST, etc.)
Cache-Control Specifies the case where the response document can be cached securely. Usually the value is  publicprivate orno-cache and many more. Public means that the document is cacheable, and Private means that the document is only served to a single user and can only use a private cache. No-cache means that the document is not cached.
Connection Whether the command browser wants to use a persistent HTTP connection. closevalue The command browser does not use persistent HTTP connections, and keep-alive means using persistent connections.
Content-Disposition Let the browser ask the user to store the response on disk with the given name
Content-Encoding Specify the encoding rules for the page when transmitting
Content-Language Express the language used by the document, such as en, en-us, ru, etc.
Content-Length Indicates the number of bytes of the response. Only useful if the browser uses a keep-alive HTTP connection
Content-Type Indicates the MIME type used by the document
Expires Indicates that the timeout expires and is removed from the cache
Last-Modified Indicate when the document was last modified. The client can cache the document and provide one in subsequent requests. If-Modified-SinceRequest header
Location Within 300 seconds, including all response addresses with a status code, the browser will automatically reconnect and retrieve the new document
Refresh Indicates how often the browser requests to update the page.
Retry-After Used in conjunction with 503 (Service Unavailable) to tell the user how long the request will get a response
Set-Cookie Indicate 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 the interface that handles the creation of HTTP headers. By using this object, developers can add new cookies or timestamps, as well as HTTP status codes and more.

The following table lists the methods used to set the HTTP response headers 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)

Returns whether the specified response header exists
4 boolean isCommitted()

Return whether the response has been submitted 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)

Add response header and int value for the specified name
9 void flushBuffer()

Write any cached content to the client
10 void reset()

Clear any data in any cache, including status codes and various response headers
11 void resetBuffer()

Clear basic cached data, excluding response headers and status codes
12 void sendError(int sc)

Send an error response to the client with 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)

Specifies the encoding set (MIME character set) of the response, such as UTF-8
17 void setContentLength(int len)

Specifies the length of the content of the response in the HTTP servlets. This method is used to set the HTTP Content-Length header
18 void setContentType(String type)

Set the type of content of the response, if the response has not yet been submitted
19 void setDateHeader(String name, long date)

Set the name and content of the response header with the specified name and value
20 void setHeader(String name, String value)

Set the name and content of the response header with the specified name and value
21 void setIntHeader(String name, int value)

Specify a value of type int to the name header
22 void setLocale(Locale loc)

Set the locale of the response, if the response has not yet been submitted
23 void setStatus(int sc)

Set the response status code

HTTP response header example

The next 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>tutorial(welookups.com)</title>
</head>
<body>
<h2>Auto refresh instance</h2>
<%
   // Set to automatically refresh every 5 seconds
   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 access it through your browser. It will display the current time of the system every 5 seconds.