Contact Us
   
   
 
   
   
 
     
 
 
JSP Tutorial
 
Book Must Read
 
 

Response object in JSP

 
 
Print   Email
 
     
 

As we know the response, response is a process to responding against it request. Response Object in JSP is used to send information, or output from web server to the user. Response Object sends output in form of stream to the browser. This can be redirecting one file to another file, response object can set cookie, set ContentType, Buffer size of page, caching control by browser, CharSet, expiration time in cache.

Response object tells browser what output has to use or display on browser, and what stream of data contain PDF, html/text, Word, Excel.

Method Return Description
addCookie(Cookie cookie) void Add specified cookies to response object 
addDateHeader(String name,long date) void Adds response header with given name and date value
addHeader(String name,String value) void Adds response header with given name and value
encodeRedirectURL(String URL) String Encode specified URL for sendRedirect method
encodeURL(String URL) String Encode specified URL with session ID, if not unchanged URL return
flushBuffer() void Forces any content in buffer to be written in client
getBufferSize() int Returns actual buffer size used for response
getCharacterEncoding() String Returns character encoding for page in response MIME type charset=iso-8859-1
getContentType() String Returns MIME type used for body in response text/html;
getOutputStream() ServletOutputStream Returns ServletOutputStream binary data stream to written in response
getWriter() PrintWriter Returns a PrintWriter object that can send character text to client
isCommitted() boolean Returns a Boolean, if response has been commited
resetBuffer() void Clear content in buffer of response without clearing header and status
sendRedirect(String location) void Sends a redirect response to client with redirect specified URL location. Contain Relative path
setBufferSize(int size) void Set a preferred buffer size for the body of response
setCharacterEncoding(String charset) void Set a character encoding for send to client response body charset=iso-8859-1
setContentLength(int length) void Set a length of content body in response
setContentType(String contentType) void Set a content Type of response being sent to client, if response is yet not committed
setHeader(String name, String value) void Set a response header with given name and value

Example of ContentType in JSP response object

setContentType.jsp

<%@ page language="java" %>
<%
response.setContentType("text/html");
%>
<html>
<head>
<title>Response object set Content type</title>
</head>

<body>
This is setting content type of JSP page
</body>
</html>

if documentation is in PDF format then setContentType as

<% response.setContentType("application/pdf"); %>

For Microsoft word document

<%
response.setContentType("application/msword");
%>

For Microsoft excel document

<%
response.setContentType("application/vnd.ms-excel");
%>

or

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>

Example of control page into cache

cacheControl.jsp

 
<%@ page language="java" %>
<%
response.setHeader("Cache-Control","no-cache"); 
/*--This is used for HTTP 1.1 --*/
response.setHeader("Pragma","no-cache");
 /*--This is used for HTTP 1.0 --*/
response.setDateHeader ("Expires", 0); 
/*---- This is used to prevents caching at the proxy server */
%>
<html>
<head>
<title>Response object in cache controlling</title>
</head>

<body>
This is no cache, Can test this page by open this page on browser and
 then open any other website after press back button on browser,
 if cacheControl.jsp is reload, it means it is not cached
</body>
</html>

Example of  sendRedirect of Response Object

sendRedirect.jsp

 
<%@ page  language="java" %>
<%
response.sendRedirect("http://yahoo.com");
///  response.sendRedirect("YouCanSpecifyYourPage.jsp");
%>
<html>
<head>
<title>Response object Send redirect</title>
</head>

<body>
This page redirects to specified URL location
</body>
</html>

 

 

 

 

 
If You Like this Article, share this

 
 
     
   
  View Forum Post  
 
Optimization of SQL queries for better performance
Example of Between in mysql
Example of sorting in mysql table
Example of join in mysql
Example of subquery in mysql
union join in mysql with example
error too many connection in mysql
calculate second highest salary
How to use select queries with array in mysql
Radio button pre selected in struts
Exception in thread "main" java.lang.NoClassDefFoundError
tree view in jsp
SSL implement in tomcat and java certificate
WARNING: Internal error flushing the buffer in release()
DynaForm validation problem in struts not working
Accessing JavaBean in servlet or servlet in javaBean.
 
     
     
   
     
     
     
 
  Privacy Policy