Create a simple HTTP server with Java?


Java Problem Overview

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?

Java Solutions

Solution 1 - Java

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".

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.

Solution 2 - Java

This is how I would go about this:

  1. Start a ServerSocket listening (probably on port 80).
  2. Once you get a connection request, accept and pass to another thread/process (this leaves your ServerSocket available to keep listening and accept other connections).
  3. Parse the request text (specifically, the headers where you will see if it is a GET or POST, and the parameters passed.
  4. Answer with your own headers (Content-Type, etc.) and the HTML.

I find it useful to use Firebug (in Firefox) to see examples of headers. This is what you want to emulate.

Try this link:

Solution 3 - Java

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.

Solution 4 - Java

If you are using the Sun JDK you can use this built in library
Look at this site on how to use.

If n ot there are several Open Source HTTP Servers here which you can embed into your software.

Solution 5 - Java

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.

Solution 6 - Java

I'm suprised this example is'nt here:

EDIT >> The above link is not reachable. Here is an excerpt from the POST example followed by the link to the HTTP examples.

 if (!conn.isOpen()) {
    	Socket socket = new Socket(host.getHostName(), host.getPort());
    BasicHttpEntityEnclosingRequest request = new
    System.out.println(">> Request URI: " + request.getRequestLine().getUri());
    httpexecutor.preProcess(request, httpproc, coreContext);
    HttpResponse response = httpexecutor.execute(request, conn, coreContext);
    httpexecutor.postProcess(response, httpproc, coreContext);
    System.out.println("<< Response: " + response.getStatusLine());
    if (!connStrategy.keepAlive(response, coreContext)) {
    } else {
    System.out.println("Connection kept alive...");

Solution 7 - Java

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

Solution 8 - Java

Jetty is a great way to easily embed an HTTP server. It supports it's own simple way to attach handlers and is a full J2EE app server if you need more functionality.

Solution 9 - Java

Embedding Tomcat is relatively painless as such things go. Here's a good StackOverflow reference about it.

Solution 10 - Java

I have implemented one link

N.B. for processing json, i used jackson. You can remove that also, if you need

Solution 11 - Java

A servlet container is definitely the way to go. If Tomcat or Jetty are too heavyweight for you, consider Winstone or TTiny.

Solution 12 - Java

Undertow is a lightweight non-blocking embedded web server that you can get up and running very quickly.

public static void main(String[] args) {
        .addHttpListener(8080, "localhost")
        .setHandler((exchange) -> exchange.getResponseSender().send("hello world"))

Solution 13 - Java

I just added a public repo with a ready to run out of the box server using Jetty and JDBC to get your project started.

Pull from github here:


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStefan KendallView Question on Stackoverflow
Solution 1 - JavaKrisView Answer on Stackoverflow
Solution 2 - JavaPhilippe SignoretView Answer on Stackoverflow
Solution 3 - Javang.View Answer on Stackoverflow
Solution 4 - JavaRomain HippeauView Answer on Stackoverflow
Solution 5 - JavaTituView Answer on Stackoverflow
Solution 6 - JavanikdeapenView Answer on Stackoverflow
Solution 7 - JavaCharles DobsonView Answer on Stackoverflow
Solution 8 - JavamaericsView Answer on Stackoverflow
Solution 9 - JavaDanView Answer on Stackoverflow
Solution 10 - JavaakashView Answer on Stackoverflow
Solution 11 - JavaMichael BorgwardtView Answer on Stackoverflow
Solution 12 - JavaBill O'NeilView Answer on Stackoverflow
Solution 13 - JavaWilliam FalconView Answer on Stackoverflow