What\'s the easiest way to create a simple HTTP server with Java? Are there any libraries in commons to facilitate this? I only need to respond to GET/POST
, and
Java 6 has a default embedded http server.
Check the thread here
By the way, if you plan to have a rest web service, here is a simple example using jersey.
Undertow is a lightweight non-blocking embedded web server that you can get up and running very quickly.
public static void main(String[] args) {
Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler((exchange) -> exchange.getResponseSender().send("hello world"))
.build().start();
}
Use Jetty. Here's a tutorial for embedding Jetty. (Here's an outdated tutorial.)
Jetty is pretty lightweight, but it does provide a servlet container, which may contradict your requirement against using an "application server".
You can embed the Jetty server into your application. Jetty allows EITHER embedded OR servlet container options.
The easiest is Simple there is a tutorial, no WEB-INF not Servlet API no dependencies. Just a simple lightweight HTTP server in a single JAR.
I wrote a tutorial explaining how to write a simple HTTP server a while back in Java. Explains what the code is doing and why the server is written that way as the tutorial progresses. Might be useful http://kcd.sytes.net/articles/simple_web_server.php
A servlet container is definitely the way to go. If Tomcat or Jetty are too heavyweight for you, consider Winstone or TTiny.