< Prev
Next >



JSP response Object





JSP inbuilt response object is used to send the data back to the user in the form of a response to the initiated request. Additionally, the response object may also be used to send cookies with a response to browser, to store cookies in it. The type of request object is javax.servlet.http.HttpServletResponse interface, which is itself a subclass of javax.servlet.ServletResponse interface.




Some useful methods available to response object.



The methods in the table below are part of javax.servlet.http.HttpServletResponse interface and its superclass javax.servlet.ServletResponse. Hence, these methods are available to the JSP implicit response object.

Methods Description
void addCookie(Cookie cookie) This method adds a Cookie to the response and sent it to browser.
void addHeader(String name, String value) This method adds a response header with a name and its value.
void addDateHeader(String name, long date) This method adds a response date header with a name and its date value.
void addIntHeader(String name, int value) This method adds a response header with a name and its int value.
void sendError(int sc, String message) This method sends an error response to client using a status code and a message.
void sendRedirect (String url) This method sends a temporary redirect response to client by specifying the URL.
String setStatus(int sc) This method sets the status code of the response.
void setHeader(String name, String value) This method sets the response header with a name and value.
void setDateHeader(String name, long date) This method sets the response header with a name and a date value.
void setIntHeader(String name, int value) This method sets the response header with a name and an int value.
void setContentType(String contentType) This method adds a MIME type to a response header.
String getHeader(String name) This method gets the value of the response header with the given name..





Using JSP response object to set the contentType attribute.


In this example, we will set the value of contentType attribute using JSP's inbuilt response object.

Response1.jsp
<html>

<head>
<title>Using JSP built-in response object</title>
</head>


<body>

<h2>Setting the contentType using response object - </h2>

<% response.setContentType("text/xml"); %>
This page will now be displayed in XML form to the user. <br/>


<i>Thank you for reading</i> <br/>
<b>Have a nice day!</b> <br/>

</body>

</html>



executing this JSP page displays the contentType of the page to "text/xml" and displayed the page in XML form to the user when the page is requested.






Using JSP reponse object to set and read a cookie value


In this example, we will create and set a cookie in the browser using JSP's inbuilt response object. In JSP, we can create a cookie using Cookie class and each cookie created has a name and a value associated with it. Let's look the code below.


AddCookie.jsp
<html>

<head>
<title>Using response object to set cookies </title>
</head>


<body>

<h2>Using the response object to add a cookie in the browser.</h2>

<%
Cookie cookie = new Cookie("Website", "DecodeJava.com");
response.addCookie(cookie);
%>

To read value in cookie, click here <a href="ReadCookie.jsp">Read Cookie</a>

</body>

</html>



and in the next JSP page we will read this cookie using JSP inbuilt request object.

ReadCookie.jsp
<html>

<head>
<title>Ceating a JSP Cookie </title>
</head>


<body>

<h2>Reading the cookies set by the request object.</h2>

<% 
Cookie[] array = request.getCookies();


for(int i=0; i<array.length; i++)
{
if(array[i].getName().equals("Name"))
{
out.println("<br/>");
out.println("Name of the cookie : " + array[i].getName() + "<br/>");
out.println("Value in cookie : " + array[i].getValue());
} 
}

%>

</body>

</html>



Executing AddCookie.jsp creates a cookie and stores it in the browser using the JSP's inbuilt response object.

AddCookie.jsp



On this page, when we click on "Read the Cookie," another page opens which reads the cookie using JSP's inbuilt request object.

ReadCookie.jsp





Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< JSP request Action
JSP out object >

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



COMING SOON
-
PYTHON