2 Minutes To Code (β Version)

JSP - 2 minute tutorial

ntroduction to 'response' implicit object

When the webserver receives an http request, it creates a response object that is implicitly available to the jsp page. The developer can use it to construct the http response that is to be sent to the http client by using the commonly used methods shown in the table blow.

response is an instance of javax.servlet.http.HttpServletResponse.

Let's see how to use response to send http status code 601 and a message to the http client.
		response.sendError(601, "Unauthorized access");

The above code will produce the following webpage :

The commonly used methods of response object are :

Method Description
void addHeader(String, String) Add the given header to http response sent to client
void addDateHeader(String, long) Convenience method to add a date as header
void addIntHeader(String, int) Convenience method to add an integer as header
boolean containsHeader(String) Checks whether given header is already present in response
void sendRedirect(String) Use to redirect browser to the given url
void setCharacterEncoding(String) Convenience method to set character encoding header
void setContentLength(int) Convenience method to set content length header
void setContentType(String) Convenience method to set content type header
void setStatus(int) Sets the http status code of response
void sendError(int) Same as above
void sendError(int, String) Same as above. Also sets http status message