Easy Tutorial
❮ Jsp File Uploading Jsp Client Request ❯

JSP Server Response

The Response object primarily sends the processed result from the JSP container back to the client. You can set the HTTP status and send data to the client, such as cookies, HTTP header information, etc., through the response variable.

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 the HTTP version information, such as HTTP/1.1, a status code, such as 200, and a short message corresponding to the status code, such as OK.

The following table summarizes the most useful parts of the HTTP1.1 response headers, which you will often encounter in network programming:

Response Header Description
Allow Specifies the request methods (GET, POST, etc.) supported by the server
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 be cached privately. No-cache means the document is not cached.
Connection Commands the browser whether to use persistent HTTP connections. The close value commands the browser not to use persistent HTTP connections, while keep-alive means using persistent connections.
Content-Disposition Lets the browser prompt the user to save the response with a given name on the disk
Content-Encoding Specifies the encoding rules for the page during transmission
Content-Language Describes 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 persistent (keep-alive) HTTP connections
Content-Type Indicates the MIME type of the document
Expires Specifies when the document expires and is 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 Within 300 seconds, includes all response addresses with a status code, and the browser will automatically reconnect and retrieve the new document
Refresh Specifies how often the browser should request an update of the page
Retry-After Used with 503 (Service Unavailable) to tell users how long to wait before requesting a response
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 the interface for handling the creation of HTTP message 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, provided by the HttpServletResponse class:

S.N. Method & Description
1 String encodeRedirectURL(String url) Encodes the URL used for 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 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 data buffered in the response, including the status code and various response headers

  1. void resetBuffer() Clears the basic buffer data, excluding response headers and status codes.
  2. void sendError(int sc) Sends an error response to the client using the specified status code, then clears the buffer.
  3. void sendError(int sc, String msg) Sends an error response to the client using the specified status code and message.
  4. void sendRedirect(String location) Sends a temporary indirect response to the client using the specified URL.
  5. void setBufferSize(int size) Sets the buffer size for the response body.
  6. void setCharacterEncoding(String charset) Specifies the character set (MIME charset) for the response, e.g., UTF-8.
  7. void setContentLength(int len) Specifies the length of the content in HTTP servlets, used to set the HTTP Content-Length header.
  8. void setContentType(String type) Sets the content type of the response if the response has not been committed.
  9. void setDateHeader(String name, long date) Sets the response header name and date with the specified name and date.
  10. void setHeader(String name, String value) Sets the response header name and content with the specified name and value.
  11. void setIntHeader(String name, int value) Sets an int value to the header with the specified name.
  12. void setLocale(Locale loc) Sets the response locale if the response has not been committed.
  13. void setStatus(int sc) Sets the response status code.

HTTP Response Header Program Example

The following example uses the setIntHeader() 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>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>
<h2>Automatic Refresh Example</h2>
<%
   // Set to refresh automatically every 5 seconds
   response.setIntHeader("Refresh", 5);
   // Get the 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 a browser. It will display the system's current time every 5 seconds.

You can also modify the above code to experiment with other methods for a deeper understanding.

❮ Jsp File Uploading Jsp Client Request ❯