<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: left;"><strong>JSP Response Object</strong> is implicitly available to the developer on Java Server Pages. This object implements <b><i>javax.servlet.ServletResponse</i></b> interface. It is useful object in setting response to serve the request. This object has a scope for that specific request. &#8220;<b><i>HttpServletResponse</i></b>&#8221; class and using this object, response parameters can be modified or set. The response object handles the output of the client. The response object is generally used by cookies. The response object is also used with HTTP Headers.<br />
Methods of response object</p>
<ul style="text-align: left;">
<li>setContentType()</li>
<li>addCookie(Cookie cookie)</li>
<li>addHeader(String name, String value)</li>
<li>containsHeader(String name)</li>
<li>setHeader(String name, String value)</li>
<li>sendRedirect(String)</li>
<li>sendError(int status_code)</li>
</ul>
<h2><b>setContentType():</b></h2>
<p>The &#8220;<b><i>setContentType()</i></b>&#8221; method of response object is used to set the MIME type and character encoding for the page.<br />
<b>Example : </b></p>
<pre class="highlight">response.setContentType("text/html");</pre>
<h2><b><br />
</b> <b>addCookie(Cookie cookie):</b></h2>
<p>This method is used to add the specified cookie to the response . If you wants to add more than one cookie, then use this method by calling it as many times as required.<br />
<b>Example :</b></p>
<pre class="highlight">response.addCookie(Cookie dineshonjava) ;</pre>
<h2><b>addHeader(String name, String value): </b></h2>
<p>This method is used to write the header to the response as pair of name and value. 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:<br />
<i><b>response.addHeader(String name, String value)</b></i><br />
<i><b>Example : </b></i></p>
<pre class="highlight">response.addHeader("User", "Dinesh") ;</pre>
<h2><b>containsHeader(String name):</b></h2>
<p>The &#8220;containsHeader()&#8221; method of response object is used to check whether the header, given as parameter, already included to the response or not. 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.<br />
<b>Example :</b></p>
<pre class="highlight">response.containsHeader(String Author)</pre>
<h2><b>setHeader(String name, String value):</b></h2>
<p>This method is used to write the header to the response as pair of name and value a 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 &#8220;<i><b>setHeader</b></i>&#8221; of response object is as follows<br />
<b><i>response.setHeader(String name, String value)</i></b><br />
<b><i>Example: </i></b></p>
<pre class="highlight">response.setHeader("Content_Type","text/html");</pre>
<h2><b>sendRedirect(String):</b></h2>
<p>This method is used to send a response which redirect client to new page. But one must note that if the JSP, in execution, has already sent page content to the client, then the &#8220;<i><b>sendRedirect()</b></i>&#8221; method of response object will not work and will fail. General syntax of sendRedirect of response object is as follows:<br />
<i><b>response.sendRedirect(String)</b></i><br />
<b>Example : </b></p>
<pre class="highlight">response.sendRedirect("https://dineshonjava.com/") ;</pre>
<h2><b>sendError(int status_code , java.lang.String msg) : </b></h2>
<p>Sends an error response to the client using the specified status code and descriptive message. If the response has already been committed, this method throws an &#8220;<b><i>IllegalStateException</i></b>&#8220;. After using this method, the response should be considered to be committed and should not be written to.</p>
<h2><b>JSP Response Object Example:</b></h2>
<pre class="highlight"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">; 
<;%@page import="java.io.PrintWriter"%>; 
<;html>; 
<;head>; 
<;title>;JSP Request Object Example<;/title>; 
<;/head>; 
<;body>; 
<;% 
 PrintWriter pw = response.getWriter(); 
 pw.print("Hello Dinesh....This is an example of JSP 'Response' object"); 
%>; 
<;/body>; 
<;/html>; 
</pre>
<p><b>Output on Browser</b>: <i><b>Hello Dinesh&#8230;.This is example of JSP &#8216;Response&#8217; object</b></i></p>
<p>As you can see above, using JSP response object, <b><i>PrintWriter </i></b>instance is created. Using this <i><b>PrintWriter</b></i> object, String response is set that displayed on the browser when JSP served a request.</p>
<p>Copy <i><b>implicitobjects </b></i>folder on webapp directory of tomcat at<br />
<i><b>C:Program Files (x86)Apache Software FoundationTomcat 7.0webapps</b></i></p>
<p>and restart server and hit the following URL<br />
<i><b>http://localhost:8080/implicitobjects/index.jsp</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/09/333.png" border="0" /></div>
<p> ;</p>
</div>
<p><b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/responseimplicitobjects.zip?attredirects=0&;d=1" target="_blank" rel="noopener">Download Example.</a></b></p>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/jsp-request-object/">Previous</a> <;<; || <a href="https://dineshonjava.com/jsp-tutorial-baby-step-to-server-pages/">Index </a>|| >;>;<a href="https://dineshonjava.com/jsp-out-object/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/jsp-request-object/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/jsp-out-object/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '333'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…