JSP

JSP Response Object

<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;"><strong>JSP Response Object<&sol;strong> is implicitly available to the developer on Java Server Pages&period; This object implements <b><i>javax&period;servlet&period;ServletResponse<&sol;i><&sol;b> interface&period; It is useful object in setting response to serve the request&period; This object has a scope for that specific request&period; &&num;8220&semi;<b><i>HttpServletResponse<&sol;i><&sol;b>&&num;8221&semi; class and using this object&comma; response parameters can be modified or set&period; The response object handles the output of the client&period; The response object is generally used by cookies&period; The response object is also used with HTTP Headers&period;<br &sol;>&NewLine;Methods of response object<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>setContentType&lpar;&rpar;<&sol;li>&NewLine;<li>addCookie&lpar;Cookie cookie&rpar;<&sol;li>&NewLine;<li>addHeader&lpar;String name&comma; String value&rpar;<&sol;li>&NewLine;<li>containsHeader&lpar;String name&rpar;<&sol;li>&NewLine;<li>setHeader&lpar;String name&comma; String value&rpar;<&sol;li>&NewLine;<li>sendRedirect&lpar;String&rpar;<&sol;li>&NewLine;<li>sendError&lpar;int status&lowbar;code&rpar;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2><b>setContentType&lpar;&rpar;&colon;<&sol;b><&sol;h2>&NewLine;<p>The &&num;8220&semi;<b><i>setContentType&lpar;&rpar;<&sol;i><&sol;b>&&num;8221&semi; method of response object is used to set the MIME type and character encoding for the page&period;<br &sol;>&NewLine;<b>Example &colon; <&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">response&period;setContentType&lpar;"text&sol;html"&rpar;&semi;<&sol;pre>&NewLine;<h2><b><br &sol;>&NewLine;<&sol;b> <b>addCookie&lpar;Cookie cookie&rpar;&colon;<&sol;b><&sol;h2>&NewLine;<p>This method is used to add the specified cookie to the response &period; If you wants to add more than one cookie&comma; then use this method by calling it as many times as required&period;<br &sol;>&NewLine;<b>Example &colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">response&period;addCookie&lpar;Cookie dineshonjava&rpar; &semi;<&sol;pre>&NewLine;<h2><b>addHeader&lpar;String name&comma; String value&rpar;&colon; <&sol;b><&sol;h2>&NewLine;<p>This method is used to write the header to the response as pair of name and value&period; If the header is already present&comma; then value is added to the existing header values&period; General syntax of addHeader&lpar;&rpar; of response object is as follows&colon;<br &sol;>&NewLine;<i><b>response&period;addHeader&lpar;String name&comma; String value&rpar;<&sol;b><&sol;i><br &sol;>&NewLine;<i><b>Example &colon; <&sol;b><&sol;i><&sol;p>&NewLine;<pre class&equals;"highlight">response&period;addHeader&lpar;"User"&comma; "Dinesh"&rpar; &semi;<&sol;pre>&NewLine;<h2><b>containsHeader&lpar;String name&rpar;&colon;<&sol;b><&sol;h2>&NewLine;<p>The &&num;8220&semi;containsHeader&lpar;&rpar;&&num;8221&semi; method of response object is used to check whether the header&comma; given as parameter&comma; already included to the response or not&period; If the named response header is set then it returns a true value&period; If the named response header is not set&comma; the value is returned as false&period;<br &sol;>&NewLine;<b>Example &colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">response&period;containsHeader&lpar;String Author&rpar;<&sol;pre>&NewLine;<h2><b>setHeader&lpar;String name&comma; String value&rpar;&colon;<&sol;b><&sol;h2>&NewLine;<p>This method is used to write the header to the response as pair of name and value a string&period; If the header is already present&comma; then the original value is replaced by the current value given as parameter in this method&period; General syntax of &&num;8220&semi;<i><b>setHeader<&sol;b><&sol;i>&&num;8221&semi; of response object is as follows<br &sol;>&NewLine;<b><i>response&period;setHeader&lpar;String name&comma; String value&rpar;<&sol;i><&sol;b><br &sol;>&NewLine;<b><i>Example&colon; <&sol;i><&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">response&period;setHeader&lpar;"Content&lowbar;Type"&comma;"text&sol;html"&rpar;&semi;<&sol;pre>&NewLine;<h2><b>sendRedirect&lpar;String&rpar;&colon;<&sol;b><&sol;h2>&NewLine;<p>This method is used to send a response which redirect client to new page&period; But one must note that if the JSP&comma; in execution&comma; has already sent page content to the client&comma; then the &&num;8220&semi;<i><b>sendRedirect&lpar;&rpar;<&sol;b><&sol;i>&&num;8221&semi; method of response object will not work and will fail&period; General syntax of sendRedirect of response object is as follows&colon;<br &sol;>&NewLine;<i><b>response&period;sendRedirect&lpar;String&rpar;<&sol;b><&sol;i><br &sol;>&NewLine;<b>Example &colon; <&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">response&period;sendRedirect&lpar;"https&colon;&sol;&sol;dineshonjava&period;com&sol;"&rpar; &semi;<&sol;pre>&NewLine;<h2><b>sendError&lpar;int status&lowbar;code &comma; java&period;lang&period;String msg&rpar; &colon; <&sol;b><&sol;h2>&NewLine;<p>Sends an error response to the client using the specified status code and descriptive message&period; If the response has already been committed&comma; this method throws an &&num;8220&semi;<b><i>IllegalStateException<&sol;i><&sol;b>&&num;8220&semi;&period; After using this method&comma; the response should be considered to be committed and should not be written to&period;<&sol;p>&NewLine;<h2><b>JSP Response Object Example&colon;<&sol;b><&sol;h2>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat; page language&equals;"java" contentType&equals;"text&sol;html&semi; charset&equals;ISO-8859-1" &NewLine; pageEncoding&equals;"ISO-8859-1"&percnt;&gt&semi; &NewLine;&lt&semi;&excl;DOCTYPE html PUBLIC "-&sol;&sol;W3C&sol;&sol;DTD HTML 4&period;01 Transitional&sol;&sol;EN" "http&colon;&sol;&sol;www&period;w3&period;org&sol;TR&sol;html4&sol;loose&period;dtd"&gt&semi; &NewLine;&lt&semi;&percnt;&commat;page import&equals;"java&period;io&period;PrintWriter"&percnt;&gt&semi; &NewLine;&lt&semi;html&gt&semi; &NewLine;&lt&semi;head&gt&semi; &NewLine;&lt&semi;title&gt&semi;JSP Request Object Example&lt&semi;&sol;title&gt&semi; &NewLine;&lt&semi;&sol;head&gt&semi; &NewLine;&lt&semi;body&gt&semi; &NewLine;&lt&semi;&percnt; &NewLine; PrintWriter pw &equals; response&period;getWriter&lpar;&rpar;&semi; &NewLine; pw&period;print&lpar;"Hello Dinesh&period;&period;&period;&period;This is an example of JSP 'Response' object"&rpar;&semi; &NewLine;&percnt;&gt&semi; &NewLine;&lt&semi;&sol;body&gt&semi; &NewLine;&lt&semi;&sol;html&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b>Output on Browser<&sol;b>&colon; <i><b>Hello Dinesh&&num;8230&semi;&period;This is example of JSP &&num;8216&semi;Response&&num;8217&semi; object<&sol;b><&sol;i><&sol;p>&NewLine;<p>As you can see above&comma; using JSP response object&comma; <b><i>PrintWriter <&sol;i><&sol;b>instance is created&period; Using this <i><b>PrintWriter<&sol;b><&sol;i> object&comma; String response is set that displayed on the browser when JSP served a request&period;<&sol;p>&NewLine;<p>Copy <i><b>implicitobjects <&sol;b><&sol;i>folder on webapp directory of tomcat at<br &sol;>&NewLine;<i><b>C&colon;Program Files &lpar;x86&rpar;Apache Software FoundationTomcat 7&period;0webapps<&sol;b><&sol;i><&sol;p>&NewLine;<p>and restart server and hit the following URL<br &sol;>&NewLine;<i><b>http&colon;&sol;&sol;localhost&colon;8080&sol;implicitobjects&sol;index&period;jsp<&sol;b><&sol;i><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;09&sol;333&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<p><b><a href&equals;"https&colon;&sol;&sol;sites&period;google&period;com&sol;a&sol;dineshonjava&period;com&sol;dineshonjava&sol;dineshonjava&sol;responseimplicitobjects&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1" target&equals;"&lowbar;blank" rel&equals;"noopener">Download Example&period;<&sol;a><&sol;b><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jsp-request-object&sol;">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jsp-tutorial-baby-step-to-server-pages&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jsp-out-object&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jsp-request-object&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jsp-out-object&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '333'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago