Interface HttpResponse


public interface HttpResponse
Interface that abstracts out the common response methods used by the system dispatcher.

Examples

200 "OK" Response

  HTTP/1.1 200 OK
  Date: Fri, 7 Oct 2022 15:19:53 GMT
  Server: Apache/2.2.14 (Win32)
  Content-Length: 88
  Content-Type: text/html
  Connection: Closed

 
  <html>
  <body>
  <h1>Hello, World!</h1>
  </body>
  </html>
 
 
This response can be created this way:
 
  httpResponse.setStatus(200);
  httpResponse.setDateHeader("Date", 123456789L);
  httpResponse.setHeader("Server", "Apache/2.2.14 (Win32)");
  httpResponse.setHeader("Content-Length", 88);
  httpResponse.setHeader("Content-Type", "text/html");
  httpResponse.setConnection("Closed");
 
 

400 "Bad Request" Response

  HTTP/1.1 404 Not Found
  Date: Sun, 18 Oct 2012 10:36:20 GMT
  Server: Apache/2.2.14 (Win32)
  Content-Length: 230
  Connection: Closed
  Content-Type: text/html; charset=iso-8859-1
 
To achieve this response, use something like:
 
  httpResponse.sendError(400);  // or .sendError(HttpStatus.BAD_REQUEST);
 
 
Since:
1.0
Version:
2022-08-30
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the output stream for the response.
    int
    Returns the HTTP status code of the response.
    Return the configured view class.
    void
    sendError(int status)
    Resets the response and sends the specified HTTP status code.
    void
    setDateHeader(String name, long date)
    Sets the specified date header.
    void
    setHeader(String name, String value)
    Sets the specified response header.
    void
    setStatus(int status)
    Sets the HTTP status code of the response.
    void
    setViewClass(Class<?> viewClass)
    Set the view class to use
  • Method Details

    • getStatus

      int getStatus()
      Returns the HTTP status code of the response.
    • setStatus

      void setStatus(int status)
      Sets the HTTP status code of the response.
      Parameters:
      status - the status code
    • setDateHeader

      void setDateHeader(String name, long date)
      Sets the specified date header.
      Parameters:
      name - header name
      date - the date as ms from UTC epoch
    • setHeader

      void setHeader(String name, String value)
      Sets the specified response header.
      Parameters:
      name - header name
      value - header value
    • sendError

      void sendError(int status) throws IOException
      Resets the response and sends the specified HTTP status code.
      Parameters:
      status - the HTTP status code to use
      Throws:
      IOException - when an error occurs
    • setViewClass

      void setViewClass(Class<?> viewClass)
      Set the view class to use
    • getViewClass

      Class<?> getViewClass()
      Return the configured view class.
    • getOutputStream

      OutputStream getOutputStream() throws IOException
      Returns the output stream for the response. The stream is automatically closed by the server.
      Returns:
      the output stream
      Throws:
      IOException