Class SimpleHttpServer

java.lang.Object
com.sun.net.httpserver.HttpServer
dev.katsute.simplehttpserver.SimpleHttpServer

public abstract class SimpleHttpServer extends HttpServer
A HttpServer with additional extensions to simplify usage.

Creating a SimpleHttpServer

A SimpleHttpServer is created in the same way was a standard HttpServer, by using one of the create methods.

Binding to a Port

A SimpleHttpServer requires a port to become accessible, the port can be defined in the create or bind methods. The backlog parameter in each of these determines the maximum amount of simultaneous connections at any given time.

Starting the Server

The server can be started by using the HttpServer.start() method.

Stopping the Server

The server can be stopped by using stop() or HttpServer.stop(int) methods.
The delay parameter determines how long the server should wait before forcefully closing the connection, by default this is 0.

Adding Pages

Pages (referred to as a context) are added by using any of the createContext methods. Content for each context is determined by using a HttpHandler. SimpleHttpServer offers some simplified handlers to handle some complex operations, documentation for those can be found in SimpleHttpHandler.
Contexts in a server are case sensitive and will resolve to the most specific context available.

Example:
If a user goes to /this/web/page and there are only handlers for /this and /this/web, then the handler for /this/web would be used because its the most specific context that exists on the server.

This behavior consequentially means that any handler added to the root / context would handle any requests that don't have a handler, since it's the most specific one available. A RootHandler can be used to mediate this issue.

Accessing the Server

Accessing on Host Machine

When a server is started it is immediately available at whatever port was set.
Example: localhost:8000, 127.0.0.1:80

Accessing on Local Network

If permitted, the server should also be accessible to all computers on your immediate internet network at your local IP.

Accessing Globally

For clients not connected to your network, they must use your public IP address.
People outside your network can not connect unless you port forward the port the server is using. This process varies depending on your ISP and can often leave your network vulnerable to attackers. It is not suggested to this unless you know what you are doing.
You can learn to port forward here (at your own risk).

Multi-threaded Server

By default the server runs on a single thread. This means that only one clients exchange can be processed at a time and can lead to long queues.
For a server to be multi-threaded the executor must be changed to one that process threads in parallel. The executor can be changed using the HttpServer.setExecutor(Executor) method on the server.
To process a fixed amount of threads you can use Executors.newFixedThreadPool(int).
To process an unlimited amount of threads you can use Executors.newCachedThreadPool().

Requests are still not being processed in parallel

Requests to the same context may not run in parallel for a user that is accessing the same page more than once. This issue is caused by the browser, where it will not send duplicate requests to the server at the same time.
This issue is better explained here.
If you still need to test multithreading then you must use an older browser like Internet Explorer or Microsoft Edge.
Since:
5.0.0
Version:
5.0.0
Author:
Katsute
See Also: