HttpServletResponse Interface  
public interface HttpServletResponse extends ServletResponse  
 
ServletResponse 
   HttpServletResponse 

The HttpServletResponse interface extends the functionality of the ServletResponse interface by providing methods to access HTTP-specific features such as HTTP headers and cookies.

Example: Using HttpServletResponse

This example uses the HttpServletResponse object that is automatically passed to the doGet() to send a response back to the user. A text field in an HTML page asks for a login name. When the Submit button is pressed, the servlet is invoked. If the login name is determined to be valid (in this case if it is equal to the String "palmer"), the HttpServletResponse object is used to open an output stream back to the client machine. If the login is invalid, the HttpServletResponse object is used to re-direct the client back to the HTML login page.

After loading the HttpServletResponse.html Web page. Type in any random String into the textfield and hit the Submit button. The servlet returns to the HTML login page. Now type in "palmer" and see what happens.

   import javax.servlet.*;  
   import javax.servlet.http.*;  
   import java.io.*;  
   public class HttpResponseServlet extends HttpServlet {
     String login;
     boolean valid = false;
     // The doGet() method is called when the user presses the
     // "Submit" button in the HttpResponseServlet.html Web page.
    
     // It reads the login name from the query string and sends
     // a response back to the client.
     public void doGet(HttpServletRequest request,
     HttpServletResponse response) throws ServletException,
     IOException {
     // Extract the login name from query string and compare it to
     // a valid entry.
     login = request.getParameter("login");
     if (login.equals("palmer")) {
     valid = true;
     }
     // If the login name is valid (equal to "palmer"), a response is
     // sent back to the client.  If the login is invalid, the
     // HttpSerlvetRequest object re-directs the client back to the
     // login HTML page.
     if (valid) {
     response.setContentType("text/html");
     PrintWriter pw = response.getWriter();
     pw.println("<HTML> <HEAD> <TITLE>HttpRequest Exmaple</TITLE>");
    
pw.println("</HEAD><BODY>");
     pw.println("Welcome ");
    
pw.println("</BODY></HTML>");
     pw.close();
     } else {
     String str = "/HttpResponseServlet.html";
    
response.sendRedirect(response.encodeRedirectURL(str));
     }
     }
   }  
  

The HttpServletResponse.html code is as follows:

  
   <HTML>
   <HEAD>
   <TITLE> HttpResponse Example </TITLE>
   </HEAD>
   <BODY>
   <FORM METHOD=GET ACTION="http://localhost:8080/servlet/HttpResponseServlet">
   Enter login name
   <INPUT TYPE=TEXT NAME=login ><BR>
   <INPUT TYPE=SUBMIT VALUE=Submit>
   </FORM>
   </BODY>
   </HTML>
addCookie()  
public void addCookie(Cookie cookie) Method
 

addCookie() adds the specified cookie to the response. More than one cookie can be added.

addDateHeader()  
public void addDateHeader(String headerName, long date) Method
 

addDateHeader() adds a response header containing the specified header name and the number of milliseconds since January 1, 1970 GMT. This method can be used to assign multiple values to a given header name.

addHeader()  
public void addHeader(String headerName, String value) Method
 

addHeader() adds a response header with the specified name and value. This method can be used to assign multiple values to a given header name.

addIntHeader()  
public void addIntHeader(String headerName, int value) Method
 

addIntHeader() adds a response header with the specified name and int value. This method can be used to assign multiple values to a given header name.

containsHeader()  
public boolean containsHeader(String headerName) Method
 

containsHeader() returns true if the response header includes the specified header name. This method can be used before calling one of the set() methods to determine if the value has already been set.

setDateHeader()  
public void setDateHeader(String headerName, long date) Method
 

setDateHeader() sets the time value of a response header for the specified header name. The time is the number of milliseconds since January 1, 1970 GMT. If the time value for the specified header has been previously set, the value passed to this method will override it.

setHeader()  
public void setHeader(String headerName, String value) Method
 

setHeader() sets a response header with the specified name and value. If the value for the specified header has been previously set, the value passed to this method will override it.

setIntHeader()  
public void setIntHeader(String headerName, int value) Method
 

setIntHeader() sets a response header with the specified name and int value. If the int value for the specified header has been previously set, the value passed to this method will override it.

sendError()  
public void sendError(int statusCode) throws IOException public void sendError(int statusCode, String message) throws IOException Method
 

sendError() sends an error response back to the client machine using the specified error status code. A descriptive message can also be provided. This method must be called before the response is committed. A response is committed when its status code and headers have been written.

The status code will be one of the following. All of these constants are defined in the HttpServletResponse interface and are public static final int:

  • SC_BAD_GATEWAY
  • SC_NOT_FOUND
  • SC_BAD_REQUEST
  • SC_NOT_IMPLEMENTED
  • SC_CONFLICT
  • SC_NOT_MODIFIED
  • SC_EXPECTATION_FAILED
  • SC_PAYMENT_REQUIRED
  • SC_FORBIDDEN
  • SC_PRECONDITION_FAILED
  • SC_GATEWAY_TIMEOUT
  • SC_PROXY_AUTHENICATION_REQUIRED
  • SC_GONE
  • SC_REQUEST_ENTITY_TOO_LARGE
  • SC_HTTP_VERSION_NOT_SUPPORTED
  • SC_REQUEST_TIMEOUT
  • SC_INTERNAL_SERVER_ERROR
  • SC_REQUEST_URI_TOO_LARGE
  • SC_LENGTH_REQUIRED
  • SC_REQUESTED_RANGE_NOT_SATISFIABLE
  • SC_METHOD_NOT_ALLOWED
  • SC_RESET_CONTENT
  • SC_MOVED_PERMANENTLY
  • SC_SEE_OTHER
  • SC_MOVED_TEMPORARILY
  • SC_SERVICE_UNAVAILABLE
  • SC_MULTIPLE_CHOICES
  • SC_UNAUTHORIZED
  • SC_NO_CONTENT
  • SC_UNSUPPORTED_MEDIA_TYPE
  • SC_NON_AUTHORITATIVE
  • SC_USE_PROXY
  • SC_NOT_ACCEPTABLE
sendRedirect()  
public void sendRedirect(String newURL) throws IOException Method
 

sendRedirect() redirects the client machine to the specified URL. This method must be called before the response is committed. A response is committed when its status code and headers have been written.

setStatus()  
public void setStatus(int statusCode) Method
 

setStatus() sets the return status code for the response. The status code should be one of the following. All of these constants are defined in the HttpServletResponse interface and are public static final int:

  • SC_ACCEPTED
  • SC_OK
  • SC_CONTINUE
  • SC_PARTIAL_CONTENT
  • SC_CREATED
  • SC_SWITCHING_PROTOCOLS
  • SC_NO_CONTENT

To send an error status to the client machine, the sendError() method should be used instead.

encodeRedirectURL()  
public String encodeRedirectURL(String url) Method
 

encodeRedirectURL() encodes the specified URL or returns it unchanged if encoding is not required. This method is used to process a URL before sending it to the sendRedirect()method.

encodeURL()  
public String encodeURL(String url) Method
 

encodeURL() encodes the specified URL by including the session ID or returns it unchanged if encoding is not needed. All URLs generated by a servlet should be processed through this method to ensure compatability with browsers that do not support cookies.

encodeRedirectUrl()  
public String encodeRedirectUrl(String url) Deprecated
 

encodeRedirectUrl() has been deprecated since Servlet 2.1 in favor of encodeRedirectURL().

encodeUrl()  
public String encodeUrl(String url) Deprecated
 

encodeUrl() has been deprecated since Servlet 2.1 in favor of encodeURL().

setStatus()  
public void setStatus(int sc, String sm) Deprecated
 

setStatus() has been deprecated in favor of sendError().