The Response Object in JSP

By: Sathya Narayana Emailed: 1674 times Printed: 2153 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The response object is an instance of a class that implements the javax.servlet.http.HttpServletResponse interface. It represents the response to be made to the client, and it makes the following methods available.

addCookie() adds the specified cookie to the response (you can add more than one cookie):

public void addCookie(Cookie cookie)

addDateHeader() adds a response header containing the specified header name and the number of milliseconds since January 1, 1970, GMT. You can use this method to assign multiple values to a given header name:

public void addDateHeader(String name,
long date)

addHeader() adds a response header with the specified name and value. You can use this method to assign multiple values to a given header name:

public void addHeader(String name,
String value)

addIntHeader() adds a response header with the specified name and int value. This method can assign multiple values to a given header name:

public void addIntHeader(String name,
int value)

containsHeader() returns true if the response header includes the specified header name. You can use this method before calling one of the set() methods to determine if the header value has already been set:

public boolean containsHeader(String name)

encodeRedirectURL() encodes the specified URL or returns it unchanged if encoding isn't required. This method is used to process a URL before sending it to the sendRedirect() method:

public String encodeRedirectURL(String url)

encodeURL() encodes the specified URL by including the session ID or returns it unchanged if encoding isn't needed. All URLs generated by a servlet should be processed through this method to ensure compatibility with browsers that don't support cookies:

public String encodeURL(String url)

flushBuffer() causes any content stored in the buffer to be written to the client. Calling this method will also commit the response, meaning that the status code and headers will be written:

public void flushBuffer()
throws java.io.IOException

getBufferSize() returns the buffer size used for the response or 0 if no buffering is used:

public int getBufferSize()

getCharacterEncoding() returns a String object containing the character encoding used in the body of the response. The default is "ISO-8859-1", which corresponds to Latin-1:

public String getCharacterEncoding()

getContentType() returns the MIME type of this response. The content type must have been set using setContentType() for this method to return a value; if no content type has been specified, this method returns null. If character encoding has been specified, the charset parameter is included in the return value. If no character encoding has been specified, the charset parameter is omitted:

public String getContentType()

getLocale() returns the locale that has been assigned to the response. By default, this will be the default locale for the server:

public java.util.Locale getLocale()

getOutputStream() returns an object ServletOutputStream object that can be used to write the response as binary data:

public ServletOutputStream getOutputStream()
throws java.io.IOException


getWriter() returns a PrintWriter object that can be used to write the response as character data:

public java.io.PrintWriter getWriter()
throws java.io.IOException

isCommitted() returns true if the response has been committed, meaning that the status code and headers have been written:

public boolean isCommitted()

reset() clears the status code and headers and any data that exists in the buffer. If the response has already been committed, calling this method will cause an exception to be thrown:

public void reset()
throws java.lang.IllegalStateException

resetBuffer() clears the content of the response buffer without clearing the headers or status code. It will throw an IllegalStateException if the response has been committed:

public void resetBuffer()

sendError() sends an error response back to the client machine using the specified error status code. You can also provide a descriptive message. This method must be called before the response is committed (in other words, before the status code and headers have been written):

public void sendError(int sc,
String msg)
throws java.io.IOException
public void sendError(int sc)
throws java.io.IOException

sendRedirect() redirects the client machine to the specified URL. This method must be called before the response is committed (in other words, before sending it to the client):

public void sendRedirect(String location)
throws java.io.IOException


setBufferSize() requests a buffer size to be used for the response. The actual buffer size will be at least this large:

public void setBufferSize(int size)

setCharacterEncoding() sets the MIME charset of the response. It overrides any previous charset setting, no matter how it was applied:

public void setCharacterEncoding(String charset)

setContentLength() sets the length of response body:

public void setContentLength(int len)

setContentType() sets the content type of the response sent to the server. The String argument specifies a MIME type and may also include the type of character encoding, for example, "text/plain; charset=ISO-8859-1":

public void setContentType(String type)

setDateHeader() sets the time value of a response header for the specified header name. The time is the number of milliseconds since January 1, 1970, GMT. If the time value for the specified header has been previously set, the value passed to this method will override it:

public void setDateHeader(String name,
long date)

setHeader() sets a response header with the specified name and value. If the value for the specified header has been previously set, the value passed to this method will override it:

public void setHeader(String name,
String value)

setIntHeader() sets a response header with the specified name and int value. If the int value for the specified header has been previously set, the value passed to this method will override it:

public void setIntHeader(String name,
int value)

setLocale() specifies the locale that will be used for the response:

public void setLocale(java.util.Locale loc)


setStatus() sets the status code and should be one of SC_ACCEPTED, SC_OK, SC_CONTINUE, SC_PARTIAL_CONTENT, SC_CREATED, SC_SWITCHING_PROTOCOLS, or SC_NO_CONTENT:

public void setStatus(int sc)

The following constants represent the status codes defined in the HTTP specification (visit http://www.w3.org/TR/html401/ for more information):

public static final int SC_CONTINUE
public static final int SC_SWITCHING_PROTOCOLS
public static final int SC_OK
public static final int SC_CREATED

public static final int SC_FOUND
public static final int SC_ACCEPTED
public static final int SC_NON_AUTHORITATIVE_INFORMATION
public static final int SC_NO_CONTENT
public static final int SC_RESET_CONTENT
public static final int SC_PARTIAL_CONTENT
public static final int SC_MULTIPLE_CHOICES
public static final int SC_MOVED_PERMANENTLY
public static final int SC_MOVED_TEMPORARILY
public static final int SC_SEE_OTHER
public static final int SC_NOT_MODIFIED

public static final int SC_USE_PROXY
public static final int SC_BAD_REQUEST
public static final int SC_UNAUTHORIZED
public static final int SC_PAYMENT_REQUIRED
public static final int SC_FORBIDDEN
public static final int SC_NOT_FOUND
public static final int SC_METHOD_NOT_ALLOWED
public static final int SC_NOT_ACCEPTABLE
public static final int SC_PROXY_AUTHENTICATION_REQUIRED
public static final int SC_REQUEST_TIMEOUT
public static final int SC_CONFLICT
public static final int SC_GONE
public static final int SC_LENGTH_REQUIRED
public static final int SC_PRECONDITION_FAILED
public static final int SC_REQUEST_ENTITY_TOO_LARGE
public static final int SC_REQUEST_URI_TOO_LONG
public static final int SC_UNSUPPORTED_MEDIA_TYPE
public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE
public static final int SC_EXPECTATION_FAILED
public static final int SC_INTERNAL_SERVER_ERROR
public static final int SC_NOT_IMPLEMENTED
public static final int SC_BAD_GATEWAY
public static final int SC_SERVICE_UNAVAILABLE
public static final int SC_GATEWAY_TIMEOUT
public static final int SC_HTTP_VERSION_NOT_SUPPORTED
public static final int SC_TEMPORARY_REDIRECT

These methods are deprecated and shouldn't be used in new code—they exist for compatibility with existing code:

public String encodeUrl(String url)
public String encodeRedirectUrl(String url)
public void setStatus(int sc,
String sm)



JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Sathya Narayana
Querying Data with the JSTL in JSP
Deleting Data in MySQL
Modifying data and using WHERE clause in MySQL
Querying the Database in MySQL
Inserting Data into Tables in MySQL
Changing the Structure of an Existing Table in MySQL
The Exception Object in JSP
The Application Object in JSP
The Session Object in JSP
The Out Object in JSP
The Response Object in JSP
The Request Object in JSP
Tag Libraries in JSP
Scriptlets and Expressions in JSP
Declarations in JSP

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
What are the different scopes in JSP?
JSP Example to connect to MS SQL database and retrieve records
Automatically Refreshing a JSP
JSP CheckBox Example
Sending Email using JSP
JSP Alert Example
JSP Program for display Date
Comparison operators in JSP
Getting Started with JSP
Retrieving a Portion of a String
Arithmetic Evaluation Using the Expression Language in JSP
Syntax For JSP Declaratives
Tags using in jsp
The Advantages of JSP
Checkbox Object
Most Emailed Articles (in JSP)
Textarea Object
Click to Activate and Use this control
The Servlet Life Cycle
JSP Example to connect to MS SQL database and retrieve records
A JSP page that gets properties from a bean
The JSP Program running first Time.
Embedding java codes in jsp sciptlets
Debugging Servlets
Document Object
Password Object
What is JSP?
A Servlet That Generates HTML
The Advantages of JSP
Retrieving a Portion of a String
Working with Status Bar Messages