Friday, March 5, 2010

JSP Response Object

In this JSP tutorial, you will learn about JSP Response object, Methods of response Object, setContentType(), addCookie(Cookie cookie), containsHeader(String name), setHeader(String name, String value), sendRedirect(String) and sendError(int status_code).

The response object denotes the HTTP Response data. The result or the information of a request is denoted with this object. The response object handles the output of the client. This contrasts with the request object. The class or the interface name of the response object is http.HttpServletResponse.

The response object is written: Javax.servlet.http.httpservletresponse.

The response object is generally used by cookies.

The response object is also used with HTTP Headers.

Methods of response Object:

There are numerous methods available for response object. Some of them are:

* setContentType()
* addCookie(Cookie cookie)
* addHeader(String name, String value)
* containsHeader(String name)
* setHeader(String name, String value)
* sendRedirect(String)
* sendError(int status_code)

List below details the usage with syntax, example and explanation of each of these methods.

setContentType():

setContentType() method of response object is used to set the MIME type and character encoding for the page.

General syntax of setContentType() of response object is as follows:


response.setContentType();

For example:


response.setContentType("text/html");

The above statement is used to set the content type as text/html dynamically.

addCookie(Cookie cookie):

addCookie() method of response object is used to add the specified cookie to the response. The addcookie() method is used to write a cookie to the response. If the user wants to add more than one cookie, then using this method by calling it as many times as the user wants will add cookies.

General syntax of addCookie() of response object is as follows:


response.addCookie(Cookie cookie)

For example:


response.addCookie(Cookie exforsys);

The above statement adds the specified cookie exforsys to the response.

addHeader(String name, String value):

addHeader() method of response object is used to write the header as a pair of name and value to the response. If the header is already present, then value is added to the existing header values.

General syntax of addHeader() of response object is as follows:


response.addHeader(String name, String value)

Here the value of string is given as second parameter and this gets assigned to the header given in first parameter as string name.

For example:


response.addHeader("Author", "Exforsys");

The output of above statement is as below:


Author: Exforsys

containsHeader(String name):

containsHeader() method of response object is used to check whether the response already includes the header given as parameter. If the named response header is set then it returns a true value. If the named response header is not set, the value is returned as false. Thus, the containsHeader method is used to test the presence of a header before setting its value. The return value from this method is a Boolean value of true or false.

General syntax of containsHeader() of response object is as follows:


response.containsHeader(String name)

Return value of the above containsHeader() method is a Boolean value true or false.

setHeader(String name, String value):

setHeader method of response object is used to create an HTTP Header with the name and value given as string. If the header is already present, then the original value is replaced by the current value given as parameter in this method.

General syntax of setHeader of response object is as follows:


response.setHeader(String name, String value)

For example:


response.setHeader("Content_Type","text/html");

The above statement would give output as


Content_Type: text/html

sendRedirect(String):

sendRedirect method of response object is used to send a redirect response to the client temporarily by making use of redirect location URL given in parameter. Thus the sendRedirect method of the response object enables one to forward a request to a new target. But one must note that if the JSP executing has already sent page content to the client, then the sendRedirect() method of response object will not work and will fail.

General syntax of sendRedirect of response object is as follows:


response.sendRedirect(String)

In the above the URL is given as string.

For example:


response.sendRedirect("http://xxx.test.com/error.html");

No comments: