Create a simple HTTP server with Java?

141,691

Solution 1

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

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: - Multithreaded Server in Java

Solution 3

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

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

Share:
141,691
Stefan Kendall
Author by

Stefan Kendall

Updated on July 05, 2022

Comments

  • Stefan Kendall
    Stefan Kendall almost 2 years

    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?

  • Stefan Kendall
    Stefan Kendall about 14 years
    Not using an "application server" was simply the requirement that I not run UNDER it. This quote sums it up quite nicely: "Don't deploy your application in Jetty, deploy Jetty in your application".
  • matbrgz
    matbrgz about 14 years
    Do you have experience with TTiny?
  • matbrgz
    matbrgz almost 12 years
    It is the Java 6 runtime, earlier versions did not ship with it.
  • Philippe Signoret
    Philippe Signoret about 8 years
    @relgukxilef Indeed. I updated to a different site with similar content. The original link is still available on Wayback Machine.
  • Samuel
    Samuel about 7 years
    Since java 8 it is blocked by default, you can allow access to the library with the accepted response here stackoverflow.com/questions/13155734/…
  • WestCoastProjects
    WestCoastProjects almost 7 years
    .. and with a ton of source files. I just want a ten line program . Where is it?
  • Kallaste
    Kallaste almost 6 years
    This should be the accepted answer, as the answer about Jetty is in direct conflict with the stated requirement of no application server. ServerSocket is as simple as it gets.
  • Dr Deo
    Dr Deo almost 5 years
    jetty makes it hard to upload files while in embedded mode since 2012 (thats 7 years btw). I haven't found a single working example of file uploads with embedded jetty. Unless you want to serve static files that you put with another tecnology. Also jetty has poor docs
  • R Sun
    R Sun over 4 years
    And to use Session, we have to use Java EE servlets API ?