[ Team LiB ] Previous Section Next Section

The HttpServletResponse Class

The HttpServletResponse object allows you to set various HTTP-related options in the response to the browser. It is a subclass of ServletResponse, so it contains all the methods you need to send an HTTP response.

Setting Header Variables

Many of the options you have already encountered, such as content type and content length, are stored in the HTTP header. For header variables that don't have a corresponding ServletResponse method, use the setHeader, setIntHeader, or setDateHeader methods:

public void setHeader(String header, String value)
public void setIntHeader(String header, int value)
public void setDateHeader(String header, Date value)

The HTTP header can contain multiple values for the same header name. When you need to store multiple header values, use addHeader, addIntHeader, or addDateHeader:

public void addHeader(String header, String value)
public void addIntHeader(String header, int value)
public void addDateHeader(String header, Date value)

The containsHeader method returns true if a header value has already been set:

public boolean containsHeader(String header)

Redirecting the Browser

When you need to tell the browser to access a different page, use the sendRedirect method:

public void sendRedirect(String redirectURL)

Which Way Did My Request Go?


A redirect is useful when you need to send the browser to a different server. A forward is much more efficient than a redirect because the redirect must first go back to the browser, which must then request the redirected page. If you are redirecting to a page in the same server, try doing a forward rather than a redirect.

Returning Status

Unless you need to send a status other than SC_OK (HTTP response code 200) indicating a normal response, you don't need to change the status code of the response. If you need to send an abnormal response code, however, you can call setStatus or setError:

public void setStatus(int statusCode)
public void setError(int statusCode)
public void setError(int statusCode, String errorMessage)
    throws IOException

Encoding URLs

When you want to send a URL to the browser, either as a link in a Web page or as a request to redirect, you should call either encodeURL or encodeRedirectURL:

public String encodeURL(String url)
public String encodeRedirectURL(String url)

The main purpose for the encoding is to handle cases when you need to store data in a session but the browser doesn't allow cookies. If the browser doesn't allow cookies, the encode routines append a session ID parameter to the URL. If the browser supports cookies, the encode routines return the URL unchanged.

Sending Cookies

When you need to send a cookie back to the browser, use the addCookie method:

public void addCookie(Cookie cookie)

After it receives them, the browser saves cookies until they expire. You don't need to keep sending the same cookies back to the browser unless their data values change.

    [ Team LiB ] Previous Section Next Section