HttpServletResponse Class

Used to generate a response to an HTTP request. This class implements the javax.servlet.http.HttpServletResponse interface defined in Version 2.5 of the Java Servlet API.

Constructors

public HttpServletResponse( javaxt.http.servlet.HttpServletRequest request, javax.servlet.http.HttpServletResponse response )

Methods

addCookie( javaxt.http.servlet.Cookie cookie ) returns void
Adds the specified cookie to the response.
addDateHeader( String name, long date ) returns void
Adds a response header with the given name and date-value. According to spec, http response headers should be allowed to have multiple values. However, this implementation does not currently allow response headers to have multiple values.
addHeader( String name, String value ) returns void
Adds a response header with the given name and value. According to spec, http response headers should be allowed to have multiple values. However, this implementation does not currently allow response headers to have multiple values.
addIntHeader( String name, int value ) returns void
Adds a response header with the given name and integer value. According to spec, http response headers should be allowed to have multiple values. However, this implementation does not currently allow response headers to have multiple values.
containsHeader( String name ) returns boolean
Returns a boolean indicating whether the named response header has already been set.
encodeRedirectURL( String url ) returns String
According to spec, this method is supposed to encode the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes logic to determine whether the session ID needs to be encoded in the URL. This is important for browsers that don't support cookies. In our case, sessions are maintained using cookies so we return the URL unchanged.
encodeURL( String url ) returns String
According to spec, this method is supposed to encode the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. This is important for browsers that don't support cookies. In our case, sessions are maintained using cookies so we return the URL unchanged.
flushBuffer( ) returns void
Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written. This method is called automatically after each http request to free up server resources. You do not need to call this method explicitly from your application.
getBufferSize( ) returns int
Returns the buffer size used for the response.
getCharacterEncoding( ) returns String
Returns the name of the character encoding used in the response.
getContentLength( ) returns Long
Returns the "Content-Length" defined in the response header. Returns null if the "Content-Length" is not defined or is less than zero.
getContentType( ) returns String
Returns the "Content-Type" defined in the response header (e.g. "text/html; charset=utf-8").
getHeader( ) returns String
Returns the raw HTTP response header.
getHeader( String name ) returns String
getLocale( ) returns java.util.Locale
Returns the locale specified for this response using the setLocale() method.
getOutputStream( ) returns javaxt.http.servlet.ServletOutputStream
Returns an output stream for writing the body of an http response. Automatically encrypts the data if the connection is SSL/TLS encrypted. Note that by default, if the request header includes a keep-alive directive, the response header will include a keep-alive response. As such, you must explicitely set the content length, set the "Connection" header to "Close", or chunk the output using chunked encoding. Example:
      //IMPORTANT: Set response headers before getting the output stream! 
        response.setContentLength(54674);
        
      //Get output stream
        java.io.OutputStream outputStream = response.getOutputStream();
      
      //Transfer bytes from an input stream to the output stream
        byte[] b = new byte[1024];
        int x=0;
        while ( (x = inputStream.read(b)) != -1) {
            outputStream.write(b,0,x);
        }
     
      //Close the input and output streams
        outputStream.close();
        inputStream.close();
   
getStatus( ) returns int
getWriter( ) returns java.io.PrintWriter
Returns a PrintWriter object that can send character text to the client.
isCommitted( ) returns boolean
Returns a boolean indicating if the response has been committed. A committed response has already had its status code and headers written.
reset( ) returns void
Clears the status code and headers. This method is called automatically after each new http request to free up server resources. You do not need to call this method explicitly from your application.
resetBuffer( ) returns void
According to spec, this method is supposed to clear the content buffer. However, this implementation doesn't use a content buffer. All content is written immediately.
sendError( int sc ) returns void
Sends an error response to the client using the specified status code and clearing the buffer.

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

scthe error status code
sendError( int sc, String msg ) returns void
Sends an error response to the client using the specified status. The server defaults to creating the response to look like an HTML-formatted server error page containing the specified message, setting the content type to "text/html", leaving cookies and other headers unmodified.

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

scthe error status code
msgthe descriptive message
sendRedirect( String location ) returns void
Sends a temporary redirect response to the client using the specified redirect location URL.
sendRedirect( String location, boolean movedPermanently ) returns void
Sends a temporary or permanent redirect response to the client using the specified redirect location URL.
setBufferSize( int size ) returns void
Sets the preferred buffer size for the body of the response. A larger buffer allows more content to be sent to the client at a time. A smaller buffer decreases server memory load and allows the client to start receiving data more quickly.

This method must be called before any response body content is written.

setCharacterEncoding( String charset ) returns void
Sets the name of the character encoding used in the response. Default is "UTF-8".
charsetString specifying the character set defined by IANA.
setContentLength( int contentLength ) returns void
Sets the "Content-Length" in the response header. Note that the "Content-Length" header is set automatically by most of the write() methods. So unless you're writing directly to the ServletOutputStream, you do not need to set this header.
setContentLength( long contentLength ) returns void
Sets the "Content-Length" in the response header. Note that the "Content-Length" header is set automatically by most of the write() methods. So unless you're writing directly to the ServletOutputStream, you do not need to set this header.
setContentType( String contentType ) returns void
Used to set/update the "Content-Type" response header (e.g. "text/html; charset=utf-8").
setDateHeader( String name, long date ) returns void
Sets a response header with the given name and date-value.
setHeader( String name, String value ) returns void
Sets a response header with the given name and value.
setIntHeader( String name, int value ) returns void
Sets a response header with the given name and integer value.
setLocale( java.util.Locale locale ) returns void
Sets the locale of the response. The locale is communicated via the "Content-Language" header and the character encoding in the "Content-Type" header.
setStatus( int sc ) returns void
setStatus( int statusCode, String statusMessage ) returns void
toString( ) returns String
Returns the raw HTTP response header.
write( String text ) returns void
Used to write a block of text in the response body. Will automatically try to gzip compress the text if "Accept-Encoding" supports gzip compression. You should only call this method once.
write( byte[] bytes ) returns void
Used to write bytes to the response body. Will automatically try to compress the byte array if "Accept-Encoding" supports gzip compression. You should only call this method once.
write( java.nio.ByteBuffer content ) returns void
write( String text, boolean compressOutput ) returns void
Used to write a block of text in the response body. You should only call this method once.
text
compressOutputSpecify whether to gzip compress the text. Note that this option will be applied only if "Accept-Encoding" supports gzip compression.
write( byte[] bytes, boolean compressOutput ) returns void
Used to write bytes to the response body. You should only call this method once.
bytesInput byte array
compressOutputSpecify whether to gzip compress the byte array. Note that this option will be applied only if "Accept-Encoding" supports gzip compression. Do not use this option if your bytes are already gzip compressed.
write( java.io.InputStream inputStream, boolean compressOutput ) returns void
Used to transfer bytes from an input stream to the response body.
inputStreamjava.io.InputStream
compressOutputSpecify whether to gzip compress the byte array. Note that this option will be applied only if "Accept-Encoding" supports gzip compression. Do not use this option if your bytes are already gzip compressed.
write( java.io.File file, String contentType, boolean useCache ) returns void
Used to write contents of a file into the response body. Automatically compresses the file content if the client supports gzip compression. You should only call this method once.