java.lang.Object
com.sun.net.httpserver.HttpServer
dev.katsute.simplehttpserver.SimpleHttpServer
A
The delay parameter determines how long the server should wait before forcefully closing the connection, by default this is 0.
Contexts in a server are case sensitive and will resolve to the most specific context available.
Example:
If a user goes to
This behavior consequentially means that any handler added to the root
Example:
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).
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
To process a fixed amount of threads you can use
To process an unlimited amount of threads you can use
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.
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 thecreate
methods.
Binding to a Port
A SimpleHttpServer requires a port to become accessible, the port can be defined in thecreate
or bind
methods.
create(int)
create(int, int)
HttpServer.create(InetSocketAddress, int)
bind(int)
bind(int, int)
HttpServer.bind(InetSocketAddress, int)
Starting the Server
The server can be started by using theHttpServer.start()
method.
Stopping the Server
The server can be stopped by usingstop()
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 thecreateContext
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:
-
Method Summary
Modifier and TypeMethodDescriptionbind
(int port) Binds the server to a port.bind
(int port, int backlog) Binds the server to a port.static SimpleHttpServer
create()
Creates an unbounded http server.static SimpleHttpServer
create
(int port) Creates an http server bounded to a port.static SimpleHttpServer
create
(int port, int backlog) Creates an http server bounded to a port.getContextHandler
(HttpContext context) Returns the handler for a given context.getContextHandler
(String context) Returns the handler for a given context.Returns a map of all the contexts registered to the server.abstract HttpServer
Returns the underlying http server.Returns a random context that doesn't yet exist on the server.getRandomContext
(String context) Returns a random context prefixed by a set context.getSession
(HttpExchange exchange) Returns the session for a given exchange.void
setSessionHandler
(HttpSessionHandler sessionHandler) Sets as session handler to use for the server.void
stop()
Stops the server immediately without waiting.Methods inherited from class com.sun.net.httpserver.HttpServer
bind, create, create, createContext, createContext, getAddress, getExecutor, removeContext, removeContext, setExecutor, start, stop
-
Method Details
-
create
Creates an unbounded http server.- Returns:
- http server
- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
create
Creates an http server bounded to a port.- Parameters:
port
- to bind to- Returns:
- http server
- Throws:
IOException
- IO exceptionBindException
- if server could not be bounded- Since:
- 5.0.0
- See Also:
-
create
Creates an http server bounded to a port.- Parameters:
port
- to bind tobacklog
- maximum amount of inbound connections at any given time- Returns:
- http server
- Throws:
IOException
- IO exceptionBindException
- if server could not be bounded- Since:
- 5.0.0
- See Also:
-
getHttpServer
Returns the underlying http server.- Returns:
- http server
- Since:
- 5.0.0
- See Also:
-
bind
Binds the server to a port.- Parameters:
port
- port to bind to- Returns:
- server address
- Throws:
IOException
- internal errorBindException
- if server could not be bounded- Since:
- 5.0.0
- See Also:
-
bind
Binds the server to a port.- Parameters:
port
- port to bind tobacklog
- maximum amount of inbound connections at any given time- Returns:
- server address
- Throws:
IOException
- internal errorBindException
- if server could not be bounded- Since:
- 5.0.0
- See Also:
-
setSessionHandler
Sets as session handler to use for the server.- Parameters:
sessionHandler
- session handler- Since:
- 5.0.0
- See Also:
-
getSessionHandler
HttpSessionHandler getSessionHandler() -
getSession
Returns the session for a given exchange.- Parameters:
exchange
- http exchange- Returns:
- session associated with an exchange
- Since:
- 5.0.0
- See Also:
-
getContextHandler
Returns the handler for a given context.- Parameters:
context
- context- Returns:
- handler for context
- Since:
- 5.0.0
- See Also:
-
getContextHandler
Returns the handler for a given context.- Parameters:
context
- http context- Returns:
- handler for context
- Since:
- 5.0.0
- See Also:
-
getContexts
Map<HttpContext,HttpHandler> getContexts()Returns a map of all the contexts registered to the server.- Returns:
- map of contexts
- Since:
- 5.0.0
- See Also:
-
getRandomContext
String getRandomContext()Returns a random context that doesn't yet exist on the server.- Returns:
- random unused context
- Since:
- 5.0.0
- See Also:
-
getRandomContext
Returns a random context prefixed by a set context.- Parameters:
context
- context to prefix- Returns:
- random unused context with prefix
- Since:
- 5.0.0
- See Also:
-
stop
void stop()Stops the server immediately without waiting.- Since:
- 5.0.0
- See Also:
-