Documentation of 'edu.rit.http.HttpResponse' Java class
HttpResponse
edu.rit.http

Class HttpResponse



  • public class HttpResponse
    extends java.lang.Object
    Class HttpResponse encapsulates an HTTP response returned to a web browser.

    Only HTTP/1.0 responses are supported. This means that only one HTTP response message can be sent over the connection to the web browser; the connection is closed after sending the HTTP response message.

    To send an HTTP response message:

    1. Create an instance of class HttpResponse, giving the socket object representing the open connection to the web browser.
       
    2. Call methods to set the status code and headers as necessary.
       
    3. Call the getPrintWriter() method, and write the entity body to the print writer that is returned.
       
    4. Call the close() method.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class and Description
      static class  HttpResponse.Status
      Enumeration HttpResponse.Status enumerates the status codes for an HTTP response message.
    • Constructor Summary

      Constructors 
      Constructor and Description
      HttpResponse(java.net.Socket theSocket)
      Construct a new HTTP response.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      void close()
      Close this HTTP response.
      java.io.PrintWriter getPrintWriter()
      Obtain the print writer for writing the entity body to this HTTP response.
      void setCharset(java.nio.charset.Charset theCharset)
      Set this HTTP response's character set.
      void setContentLength(int theContentLength)
      Set this HTTP response's content length.
      void setContentType(java.lang.String theContentType)
      Set this HTTP response's content type.
      void setHeader(java.lang.String theHeaderName, java.lang.String theHeaderValue)
      Set the given header in this HTTP response.
      void setStatusCode(HttpResponse.Status theStatusCode)
      Set this HTTP response's status code.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HttpResponse

        public HttpResponse(java.net.Socket theSocket)
        Construct a new HTTP response. The response is written to the output stream of the given socket.
        Parameters:
        theSocket - Socket.
        Throws:
        java.lang.NullPointerException - (unchecked exception) Thrown if theSocket is null.
    • Method Detail

      • setStatusCode

        public void setStatusCode(HttpResponse.Status theStatusCode)
        Set this HTTP response's status code. If not set, the default status code is STATUS_200_OK.
        Parameters:
        theStatusCode - Status code.
        Throws:
        java.lang.NullPointerException - (unchecked exception) Thrown if theStatusCode is null.
        java.lang.IllegalStateException - (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
      • setContentType

        public void setContentType(java.lang.String theContentType)
        Set this HTTP response's content type. If not set, the default content type is "text/html".
        Parameters:
        theContentType - Content type.
        Throws:
        java.lang.NullPointerException - (unchecked exception) Thrown if theContentType is null.
        java.lang.IllegalArgumentException - (unchecked exception) Thrown if theContentType is zero length.
        java.lang.IllegalStateException - (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
      • setCharset

        public void setCharset(java.nio.charset.Charset theCharset)
        Set this HTTP response's character set. If not set, the default character set is the platform default character set.
        Parameters:
        theCharset - Character set.
        Throws:
        java.lang.NullPointerException - (unchecked exception) Thrown if theCharset is null.
        java.lang.IllegalStateException - (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
      • setContentLength

        public void setContentLength(int theContentLength)
        Set this HTTP response's content length. If not set, the default is to omit the Content-Length header; closing this HTTP response marks the end of the entity body.
        Parameters:
        theContentLength - Content length.
        Throws:
        java.lang.IllegalArgumentException - (unchecked exception) Thrown if theContentLength is less than 0.
        java.lang.IllegalStateException - (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
      • setHeader

        public void setHeader(java.lang.String theHeaderName,
                              java.lang.String theHeaderValue)
        Set the given header in this HTTP response.
        Parameters:
        theHeaderName - Header name.
        theHeaderValue - Header value.
        Throws:
        java.lang.NullPointerException - (unchecked exception) Thrown if theHeaderName or theHeaderValue is null.
        java.lang.IllegalArgumentException - (unchecked exception) Thrown if theHeaderName or theHeaderValue is zero length.
        java.lang.IllegalStateException - (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
      • getPrintWriter

        public java.io.PrintWriter getPrintWriter()
                                           throws java.io.IOException
        Obtain the print writer for writing the entity body to this HTTP response. As a side effect, the HTTP response headers are written to the socket output stream.
        Returns:
        Print writer.
        Throws:
        java.io.IOException - Thrown if an I/O error occurred.
      • close

        public void close()
                   throws java.io.IOException
        Close this HTTP response. If necessary, the HTTP response headers are written to the socket output stream.
        Throws:
        java.io.IOException - Thrown if an I/O error occurred.

DMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.