Search code examples
javahttppostget

Create a simple HTTP server with Java?


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 I can't use an application server.

What's the easiest way to accomplish this?


Solution

  • Use Jetty. Here's the official example 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".

    Jetty 12 has separated the Servlet requirement from the Core Jetty Server, you are now able to use Jetty Server without any Servlet requirement, as long as you use Jetty 12 without an ee# environment.

    You can embed the Jetty server into your application. Jetty allows EITHER embedded OR servlet container options.

    Here is one more quick get started tutorial along with the source code, and the Jetty curated list of embedded examples.