HttpServletResponse

Java Servlet Programming, 2nd Edition > B. HTTP Servlet API Quick Reference > HttpServletResponse

 
< BACKCONTINUE >
HttpServletResponse

Synopsis

Interface Name: javax.servlet.http.HttpServletResponse

Superinterface: javax.servlet.ServletResponse

Immediate Subinterfaces: None

Implemented By: None

Availability: Servlet API 1.0 and later

Description

HttpServletResponse extends the ServletResponse class to allow manipulation of HTTP protocol-specific data, including response headers and status codes. It also defines a series of constants that represent various HTTP status codes and includes helper functions for session-tracking operations.

Interface Declaration

public interface HttpServletResponse extends javax.servlet.ServletResponse {   // Constants   public static final int SC_ACCEPTED;   public static final int SC_BAD_GATEWAY;   public static final int SC_BAD_REQUEST;   public static final int SC_CONFLICT;   public static final int SC_CONTINUE;                      // New in 2.0   public static final int SC_CREATED;   public static final int SC_EXPECTATION_FAILED;            // New in 2.2   public static final int SC_FORBIDDEN;   public static final int SC_GATEWAY_TIMEOUT;               // New in 2.0   public static final int SC_GONE;                          // New in 2.0   public static final int SC_HTTP_VERSION_NOT_SUPPORTED;    // New in 2.0   public static final int SC_INTERNAL_SERVER_ERROR;   public static final int SC_LENGTH_REQUIRED;               // New in 2.0   public static final int SC_METHOD_NOT_ALLOWED;            // New in 2.0   public static final int SC_MOVED_PERMANENTLY;   public static final int SC_MOVED_TEMPORARILY;   public static final int SC_MULTIPLE_CHOICES;              // New in 2.0   public static final int SC_NO_CONTENT;   public static final int SC_NON_AUTHORITATIVE_INFORMATION; // New in 2.0   public static final int SC_NOT_ACCEPTABLE;                // New in 2.0   public static final int SC_NOT_FOUND;   public static final int SC_NOT_IMPLEMENTED;   public static final int SC_NOT_MODIFIED;   public static final int SC_OK;   public static final int SC_PARTIAL_CONTENT;               // New in 2.0   public static final int SC_PAYMENT_REQUIRED;              // New in 2.0   public static final int SC_PRECONDITION_FAILED;           // New in 2.0   public static final int SC_PROXY_AUTHENTICATION_REQUIRED; // New in 2.0   public static final int SC_REQUEST_ENTITY_TOO_LARGE;      // New in 2.0   public static final int SC_REQUEST_TIMEOUT;               // New in 2.0   public static final int SC_REQUEST_URI_TOO_LONG;          // New in 2.0   public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE;// New in 2.2   public static final int SC_RESET_CONTENT;                 // New in 2.0   public static final int SC_SEE_OTHER;                     // New in 2.0   public static final int SC_SERVICE_UNAVAILABLE;   public static final int SC_SWITCHING_PROTOCOLS;           // New in 2.0   public static final int SC_UNAUTHORIZED;   public static final int SC_UNSUPPORTED_MEDIA_TYPE;        // New in 2.0   public static final int SC_USE_PROXY;                     // New in 2.0   // Methods   public abstract void addCookie(Cookie cookie);            // New in 2.0   public abstract void addDateHeader(String name, long date);// New in 2.2   public abstract void addHeader(String name, String value);// New in 2.2   public abstract void addIntHeader(String name, int value);// New in 2.2   public abstract boolean containsHeader(String name);   public abstract String encodeRedirectUrl(String url);     // Deprecated   public abstract String encodeRedirectURL(String url);     // New in 2.1   public abstract String encodeUrl(String url);             // Deprecated   public abstract String encodeURL(String url);             // New in 2.1   public abstract void sendError(int sc)     throws IOException, IllegalStateException;   public abstract void sendError(int sc, String msg)     throws IOException, IllegalStateException;   public abstract void sendRedirect(String location)     throws IOException, IllegalStateException;   public abstract void setDateHeader(String name, long date);   public abstract void setHeader(String name, String value);   public abstract void setIntHeader(String name, int value);   public abstract void setStatus(int sc);   public abstract void setStatus(int sc, String sm);        // Deprecated }

Constants

Appendix D contains complete descriptions of all the SC_XXX status codes.

Methods

addCookie()

public abstract void addCookie(Cookie cookie)
Description

Adds the specified cookie to the response. Additional cookies can be added with repeated calls to addCookie( ). Because cookies are sent using HTTP headers, they should be added to the response before the response is committed. Browsers are required to accept only 20 cookies per site, 300 total per user, and they can limit each cookie's size to 4096 bytes.

addDateHeader()

public abstract void addDateHeader(String name, long date)
Description

Adds a header with the given name and date value. The method accepts the date as a long that represents the number of milliseconds since midnight, January 1, 1970, GMT. This method allows response headers to have multiple values. This method was introduced in Servlet API 2.2.

addHeader()

public abstract void addHeader(String name, String value)
Description

Adds a header with the given name and value. This method allows response headers to have multiple values. This method was introduced in Servlet API 2.2.

addIntHeader()

public abstract void addIntHeader(String name, int value)
Description

Adds a header with the given name and int value. The method accepts the date as a long that represents the number of milliseconds since midnight, January 1, 1970, GMT. This method allows response headers to have multiple values. This method was introduced in Servlet API 2.2.

containsHeader()

public abstract boolean containsHeader(String name)
Description

Returns true if the named header has already been set, false if not.

encodeRedirectURL()

public abstract String encodeRedirectUrl(String url) public abstract String encodeRedirectURL(String url)
Description

Returns the specified URL encoded (rewritten) to include the session ID. If encoding is not needed or not supported, the method leaves the URL unchanged. The rules used to decide when and how to encode a URL are server-specific. This method may use different rules than encodeURL( ). To enable session tracking, all URLs passed to the sendRedirect( ) method should be run through this method. The encodeRedirectUrl( ) method was introduced in Servlet API 2.0, then deprecated in Servlet API 2.1 with the introduction of the more standardly named encodeRedirectURL( ) method.

encodeURL()

public abstract String encodeUrl(String url) public abstract String encodeURL(String url)
Description

Returns the specified URL encoded (rewritten) to include the session ID. If encoding is not needed or not supported, the method leaves the URL unchanged. The rules used to decide when and how to encode a URL are server-specific. To enable session tracking, all URLs emitted by a servlet should be run through this method. The encodeUrl( ) method was introduced in Servlet API 2.0, then deprecated in Servlet API 2.1 with the introduction of the more standardly named encodeURL( ) method.

sendError()

public abstract void sendError(int sc)   throws IOException, IllegalStateException public abstract void sendError(int sc, String msg)   throws IOException, IllegalStateException
Description

These methods are similar to setStatus( ), except that they are used when the status code indicates an error during the handling of the request and the servlet would like the server to generate an appropriate error page. This method should be called before the response is committed, otherwise it will throw an IllegalStateException. This method performs an implicit reset on the response buffer before generating the error page. Headers set before sendError( ) should remain set.

sendRedirect()

public abstract void sendRedirect(String location)   throws IOException, IllegalStateException
Description

Redirects the response to the specified location, automatically setting the status code and Location header. The default implementation also writes a short response body that contains a hyperlink to the new location, to support browsers without redirect capabilities. Consequently, do not write your own response body when using this method. The HTTP specification dictates that all redirect URLs must be absolute; however, beginning with Servlet API 2.2 this method can accept a relative URL the server will transform the URL to an absolute form (automatically prepending the current protocol, server, and port but not the context path; do that yourself if necessary) before sending it to the client. This method should be called before the response is committed, otherwise it will throw an IllegalStateException. This method performs an implicit reset on the response buffer before generating the redirect page. Headers set before sendRedirect( ) should remain set.

setDateHeader()

public abstract void setDateHeader(String name, long date)
Description

Sets the value of the named header as a String specifying a particular date and time. The method accepts the date value as a long that represents the number of milliseconds since midnight, January 1, 1970, GMT. If the header has already been set, the new value overwrites all previous values.

setHeader()

public abstract void setHeader(String name, String value)
Description

Sets the value of the named header as a String. The name is case insensitive (as with all header-related methods). If the header has already been set, the new value overwrites all previous values. This method can set any header type. Headers should always be set before the response is committed.

setIntHeader()

public abstract void setIntHeader(String name, int value)
Description

Sets the value of the named header as an int. If the header has already been set, the new value overwrites all previous values.

setStatus()

public abstract void setStatus(int sc) public abstract void setStatus(int sc, String sm)
Description

Sets the HTTP status code. The code can be specified using a numeric value or by using the SC_XXX codes defined within HttpServletResponse. A custom HTTP protocol error message can be provided as a second parameter; however, this should never be done and the version of this method that accepts a String has been deprecated as of Servlet API 2.1. The status code should be set before the response is committed, otherwise the call is ignored.


Last updated on 3/20/2003
Java Servlet Programming, 2nd Edition, © 2001 O'Reilly

< BACKCONTINUE >


Java servlet programming
Java Servlet Programming (Java Series)
ISBN: 0596000405
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net