JSP

JSP Response Object

JSP Response Object is implicitly available to the developer on Java Server Pages. This object implements javax.servlet.ServletResponse interface. It is useful object in setting response to serve the request. This object has a scope for that specific request. “HttpServletResponse” 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.
Methods of response object
  • setContentType()
  • addCookie(Cookie cookie)
  • addHeader(String name, String value)
  • containsHeader(String name)
  • setHeader(String name, String value)
  • sendRedirect(String)
  • sendError(int status_code)

setContentType():

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

response.setContentType("text/html");


addCookie(Cookie cookie):

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.
Example :

response.addCookie(Cookie dineshonjava) ;

addHeader(String name, String value):

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:
response.addHeader(String name, String value)
Example :

response.addHeader("User", "Dinesh") ;

containsHeader(String name):

The “containsHeader()” 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.
Example :

response.containsHeader(String Author)

setHeader(String name, String value):

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 “setHeader” of response object is as follows
response.setHeader(String name, String value)
Example:

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

sendRedirect(String):

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 “sendRedirect()” method of response object will not work and will fail. General syntax of sendRedirect of response object is as follows:
response.sendRedirect(String)
Example :

response.sendRedirect("https://www.dineshonjava.com/") ;

sendError(int status_code , java.lang.String msg) :

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 “IllegalStateException“. After using this method, the response should be considered to be committed and should not be written to.

JSP Response Object Example:

<%@ 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>

Output on Browser: Hello Dinesh….This is example of JSP ‘Response’ object

As you can see above, using JSP response object, PrintWriter instance is created. Using this PrintWriter object, String response is set that displayed on the browser when JSP served a request.

Copy implicitobjects folder on webapp directory of tomcat at
C:Program Files (x86)Apache Software FoundationTomcat 7.0webapps

and restart server and hit the following URL
http://localhost:8080/implicitobjects/index.jsp

 

Download Example.

 

<<Previous <<   || Index ||   >>Next >>
Previous
Next
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…

2 years ago

Decorator Pattern using Lambda

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

2 years ago

Delegating pattern using lambda

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

2 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;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

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

2 years ago

Scheduler @Scheduled Annotation Spring Boot

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

2 years ago