Create a simple HTTP server with Java?
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:
- Start a
ServerSocket
listening (probably on port 80). - 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). - Parse the request text (specifically, the headers where you will see if it is a GET or POST, and the parameters passed.
- 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.
By the way, if you plan to have a rest web service, here is a simple example using jersey.
Stefan Kendall
Updated on July 05, 2022Comments
-
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 about 14 yearsNot 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 about 14 yearsDo you have experience with TTiny?
-
matbrgz almost 12 yearsIt is the Java 6 runtime, earlier versions did not ship with it.
-
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 about 7 yearsSince java 8 it is blocked by default, you can allow access to the library with the accepted response here stackoverflow.com/questions/13155734/…
-
WestCoastProjects almost 7 years.. and with a ton of source files. I just want a ten line program . Where is it?
-
Kallaste almost 6 yearsThis 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 almost 5 yearsjetty 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 over 4 yearsAnd to use Session, we have to use Java EE servlets API ?