Class HttpServer
- java.lang.Object
-
- edu.rit.http.HttpServer
-
public abstract class HttpServer extends java.lang.ObjectClass HttpServer provides a lightweight HTTP/1.0 server. The HTTP server is designed to be embedded inside another application.When constructed, the HTTP server starts a thread listening for connections to a given host and port. When a web browser sets up a connection, the HTTP server calls the process() method to process the request. This is an abstract method that must be overridden in a subclass. The process() method's arguments are an HttpRequest object from which the method reads the HTTP request message and an HttpResponse object to which the method writes the HTTP response message.
The HTTP server assumes that the process() method will not perform any lengthy processing and will not block the calling thread. If the HTTP request requires lengthy processing or requires blocking, the process() method should spawn a separate thread to process the request. A thread pool may prove useful; see package java.util.concurrent.
When a client opens a socket connection to the HTTP server, the server places a two-second timeout on reading the socket. If the client does not send an HTTP request message before the timeout, the HttpRequest's isValid() method returns false.
-
-
Constructor Summary
Constructors Constructor and Description HttpServer(java.net.InetSocketAddress address)Construct a new HTTP server.HttpServer(java.net.InetSocketAddress address, Logger logger)Construct a new HTTP server.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidclose()Close this HTTP server.java.net.InetSocketAddressgetAddress()Obtain the host and port to which this HTTP server is listening for connections.
-
-
-
Constructor Detail
-
HttpServer
public HttpServer(java.net.InetSocketAddress address) throws java.io.IOExceptionConstruct a new HTTP server. The HTTP server will print error messages on the standard error.- Parameters:
address- Host and port to which the HTTP server will listen for connections.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
HttpServer
public HttpServer(java.net.InetSocketAddress address, Logger logger) throws java.io.IOExceptionConstruct a new HTTP server. The HTTP server will print error messages using the given logger.- Parameters:
address- Host and port to which the HTTP server will listen for connections.logger- Error message logger. If null, the HTTP server will print error messages on the standard error.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
-
Method Detail
-
getAddress
public java.net.InetSocketAddress getAddress()
Obtain the host and port to which this HTTP server is listening for connections.- Returns:
- Host and port.
-
close
public void close() throws java.io.IOExceptionClose this HTTP server.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
-
DMelt 3.0 © DataMelt by jWork.ORG