Reputation: 67822
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?
Upvotes: 68
Views: 148326
Reputation: 14458
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.
Upvotes: 48
Reputation: 156434
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.
Upvotes: 1
Reputation: 829
I have implemented one link
N.B. for processing json, i used jackson. You can remove that also, if you need
Upvotes: 1
Reputation: 582
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();
}
Upvotes: 0
Reputation: 14336
This is how I would go about this:
ServerSocket
listening (probably on port 80).ServerSocket
available to keep listening and accept other connections).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
Upvotes: 37
Reputation: 1601
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());
conn.bind(socket);
}
BasicHttpEntityEnclosingRequest request = new
BasicHttpEntityEnclosingRequest("POST",
"/servlets‐examples/servlet/RequestInfoExample");
request.setEntity(requestBodies[i]);
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());
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println("==============");
if (!connStrategy.keepAlive(response, coreContext)) {
conn.close();
} else {
System.out.println("Connection kept alive...");
}
http://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/
Upvotes: 4
Reputation: 9813
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: https://github.com/waf04/WAF-Simple-JAVA-HTTP-MYSQL-Server.git
Upvotes: -1
Reputation: 212
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.
Upvotes: 5
Reputation: 31
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
Upvotes: 3
Reputation: 7189
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.
Upvotes: 23
Reputation: 346290
A servlet container is definitely the way to go. If Tomcat or Jetty are too heavyweight for you, consider Winstone or TTiny.
Upvotes: 0
Reputation: 24375
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.
Upvotes: 8