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.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)