Class HttpServer

java.lang.Object
net.targetr.wtm3.net.http.HttpServer

public class HttpServer extends Object
Accepts HTTP and HTTPS connections on one port, starting a virtual-thread HttpHandler for each socket.
Author:
Dr Michael Gardiner
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new web server suitable for listening to a port for connections from web clients.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if the web server has entered the stopping state.
    void
    listen(int port, int backlog, InetAddress addr)
    Starts listening on a specific port for connection from web clients.
    void
    Sets the responder that will process requests and generate responses.
    void
    Closes the listener and waits up to one second for its loop to stop.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HttpServer

      public HttpServer()
      Creates a new web server suitable for listening to a port for connections from web clients.
  • Method Details

    • setResponder

      public void setResponder(ResponseGenerator responder)
      Sets the responder that will process requests and generate responses.
      Parameters:
      responder - For responding to requests.
    • listen

      public void listen(int port, int backlog, InetAddress addr) throws IOException, InterruptedException
      Starts listening on a specific port for connection from web clients.

      This method blocks until stopping.

      Parameters:
      port - The port to listen for HTTP and HTTPS connections.
      backlog - requested maximum number of pending connections
      addr - A specific network interface address to listen on or null for all.
      Throws:
      IOException - if binding or closing the listening socket fails
      InterruptedException - declared for compatibility; not currently thrown
    • isStopping

      public boolean isStopping()
      Returns true if the web server has entered the stopping state.
      Returns:
      true if stopping.
    • stop

      public void stop()
      Closes the listener and waits up to one second for its loop to stop. Existing client connections are not closed by this method.