List of useful methods of ServletResponse and HttpServletResponse

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

  1. Java.io.OutputStream for binary data

  2. 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

  1. ServletOutputStream getOutputStream ()

  2. PrintWriter getWriter()

  3. void flushBuffer()

  4. int getBufferSize() – 0 if no buffering is used

  5. boolean isCommited()

  6. void reset()

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

    2. IllegalStateException is response is already commied

  7. void resetBuffer()

    1. Clear any data on buffer

    2. IllegalStateExceptio if response has be committed or buffering is not used

  8. void setBufferSize (int size)

 

Data transmission related methods of ServletResponse

  1. void setCharacterEncoding (String enc)

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

  2. String getCharacterEncoding()

  3. void setContentype (String type)

    • Eg: text/html, application/xml

    • Text/html;charset-UTF-8

  4. String getContentType()

  5. 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

  6. Locale getLocale()

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

  1. HTTP headers and status codes

  2. URL rewriting used for session management

  3. Attributes to supply cookies to the client

 

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

  1. void addHeader(String name, String value)

  2. void setHeader (String name, String value)

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

  3. Boolean containsHeader (String name)

  4. void addIntHeader (String name, int value)

  5. void setIntHeader (String name, int value)

  6. void addDataHeader (String name, long value)

  7. void setDateHeader(String name, long value)

 

HTTP status codes and redirection related methods

  1. void setStatus (int code)

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

  2. 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

  3. void sendError (int code, String message)

    • Provides an additional error message

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

  1. String encodeURL (String url)

  2. 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.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!