JSP Tutorial - JSP Server Response








With JSP we can access a Web server HTTP response to the browser.

The response object is an instance of a javax.servlet.http.HttpServletResponse object.

We can use the following methods to set HTTP response header in your servlet program.

MethodDescription
String encodeRedirectURL(String url)Encodes the URL in the sendRedirect method or, if encoding is not needed, returns the URL unchanged.
String encodeURL(String url)Encodes the URL by including the session ID, or, if encoding is not needed, returns the URL unchanged.
boolean containsHeader(String name)Returns a boolean indicating whether the named header has been set.
boolean isCommitted()Returns a boolean indicating if the response has been committed.
void addCookie(Cookie cookie)Adds the cookie to the response.
void addDateHeader(String name, long date)Adds a response header with the given name and date-value.
void addHeader(String name, String value)Adds a response header with the given name and value.
void addIntHeader(String name, int value)Adds a response header with the given name and integer value.
void flushBuffer()Forces content in the buffer to output to the client.
void reset()Clears data in the buffer, the status code and headers.
void resetBuffer()Clears the buffer content in the response without clearing headers or status code.
void sendError(int sc)Sends an error response to the client with the specified status code and clear the buffer.
void sendError(int sc, String msg)Sends an error response to the client using the specified status.
void sendRedirect(String location)Sends a temporary redirect response to the client using the specified redirect location URL.
void setBufferSize(int size)Sets the preferred buffer size for the response body.
void setCharacterEncoding(String charset)Sets the character encoding of the response, for example, to UTF-8.
void setContentLength(int len)Sets the length of the content body in the response(HTTP Content-Length header).
void setContentType(String type)Sets the content type of the response, if the response has not been committed yet.
void setDateHeader(String name, long date)Sets a response header with the given name and date-value.
void setHeader(String name, String value)Sets a response header with the given name and value.
void setIntHeader(String name, int value)Sets a response header with the given name and integer value.
void setLocale(Locale loc) Sets the locale of the response, if the response has not been committed yet.
void setStatus(int sc)Sets the status code for this response.




HTTP Header Response Example

The following code shows how to use setIntHeader() method to set Refresh header to simulate a digital clock.

<%@ page import="java.io.*,java.util.*" %>
<html>
<body>
<center>
<%
   // Set refresh, autoload time as 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 is: " + CT + "\n");
%>
</center>
</body>
</html>




HTTP Status Code Example

The following code shows how to send 407 error code to the client browser.

<html>
<body>
<%
   response.sendError(407, "Need authentication!!!" );
%>
</body>
</html>

Page Redirecting

Page redirection is used when a document moves to a new location.

We can do page redirecting using method sendRedirect() from response object.

It has the following signature:

public void response.sendRedirect(String location)
throws IOException
<%@ page import="java.io.*,java.util.*" %>
<html>
<body>
<h1>Page Redirection</h1>
<%
   String site = new String("http://www.java2s.com");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site); 
%>
</body>
</html>

Auto Refresh

We can use method setIntHeader() from response object to set interval for page refresh.

public void setIntHeader(String header, int headerValue)

This method can send back header "Refresh" to the browser along with an integer indicating time interval in seconds.

The following code shows how to do page refresh.

<%@ page import="java.io.*,java.util.*" %>
<html>
<body>
<%
   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>