Social Groups

ServletResponse Interface

ServletResponse encapsulates basic properties of a response independent of protocol and provide output streams to write content to client, independent of protocol.

There are two response streams

Java.io.OutputStream for binary data

Java.io.writer for character data

Note:

Only one of these two may be used or will get IllegalStateException.

Once the container start sending data to the client (or through explicit flush or close) you cannot change the data sent or the status codes, even if you find any need for error data. You can either try to prolong the send time with a larger buffer size at the cost of memory and performance, or use a response wrapper between the component and container.

Stream related methods in ServletResponse

ServletOutputStream getOutputStream ()

PrintWriter getWriter()

void flushBuffer()

int getBufferSize() – 0 if no buffering is used

boolean isCommited()

void reset()

Clears any data in buffered output and any headers or status codes

IllegalStateException is response is already commied

void resetBuffer()

Clear any data on buffer

IllegalStateExceptio if response has be committed or buffering is not used

void setBufferSize (int size)

Data transmission related methods of ServletResponse

void setCharacterEncoding (String enc)

Eg- UTF-8, UTF-16, and default ISO-8858-1

String getCharacterEncoding()

void setContentype (String type)

Eg: text/html, application/xml

Text/html;charset-UTF-8

String getContentType()

void setLocale (Locale locale)

Locales are mapped to encoding often; lower if the character encoding has been set explicitly . The encoding implied by the locale is ignored

Locale getLocale()

void setContentLength(int size)

Optional, but advised

No getter counter part

Note:

Encoding can be set directly by setCharacterEncoding, and indirectly as part of setContentType() or setLocale()

Setting encoding directly or indirectly only has an effect before the setWriter() method id first called. Changing the character encoding will not help much after the writer is established using a character encoding

Character encoding is independent of the binary output stream and may be changed after a call to getOutputStream (), but before the response is commited

HtpServletResponse Interface

HtpServletResponse Interface adds support for:

HTTP headers and status codes

URL rewriting used for session management

Attributes to supply cookies to the client

Methods for adding Http headers if involved after the response has been committed are ignored

void addHeader(String name, String value)

void setHeader (String name, String value)

setHeader overwrites any existing values, while addHeader appends to any existing values

Boolean containsHeader (String name)

void addIntHeader (String name, int value)

void setIntHeader (String name, int value)

void addDataHeader (String name, long value)

void setDateHeader(String name, long value)

HTTP status codes and redirection related methods

void setStatus (int code)

Code should be one of constants declared in HttpServletResponse interface. Eg: SC-ACCEPTED (200)

void sendError (int code)

Though technically not restricted, it is meaninfull to send one of 400 or 500 code

Container clears the buffer. So none of your response is commited and presents the client with an error message if one is configured

void sendError (int code, String message)

Provides an additional error message

void sendRedirect (String path)

Set the status codeto 302(‘found’) or 307 (‘Temporary redirect’) , clears the buffer and signals to the client to redirect to the given path url

URL Rewriting related methods:

String encodeURL (String url)

String encodeRedirectURL (String url)

Note

Both encode url if applicable

If client currently has a valid session and session tracking is turned on

Method for transferring the session ID is in the URL, and not via cookies or secure certificates

encodeRedirectURL url can be used in sendRedirect() and encodeURL can be used in all other cases

Cookies: We can add a cookie to the response using:

void addCookie(Cookie cookie)

Container will include Http code for the client to create a new cookie with the supplied settings

If client support cookies,cookie will be available on client machine until the expire date we set.

You are here

This is a personal technical blog where we share our understanding on various concepts and is neither an official page or documentation for the products described here, nor the official views of the companies we work with.

Keywords used in this website are trademarks of their respective owners. This website is not affiliated with Oracle™ and/or any of the JEE frameworks like Spring™, Struts™, Hibernate™ and JSF™.

All contents and materials are provided freely without any warranty or liability and nothing within the site should be considered as professional advice. In any doubt, please ask, and we will try to help you based on our knowledge. Please let us know if you feel anything is not right here (including any copyright violation) and we will act upon it as fast as we can.