Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java class for embedded HTTP server in Swing app

I wish to embed a very light HTTP server in my Java Swing app which just accepts requests, performs some actions, and returns the results.

Is there a very light Java class that I can use in my app which listens on a specified port for HTTP requests and lets me handle requests?

Note, that I am not looking for a stand-alone HTTP server, just a small Java class which I can use in my app.

like image 849
CodeAndCats Avatar asked Jul 27 '09 03:07

CodeAndCats


4 Answers

Since Java 6, the JDK contains a simple HTTP server implementation.

Example usage:

import java.io.IOException; import java.io.OutputStream; import java.net.InetSocketAddress; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.concurrent.Executors;  import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer;  public class HttpServerDemo {   public static void main(String[] args) throws IOException {     InetSocketAddress addr = new InetSocketAddress(8080);     HttpServer server = HttpServer.create(addr, 0);      server.createContext("/", new MyHandler());     server.setExecutor(Executors.newCachedThreadPool());     server.start();     System.out.println("Server is listening on port 8080" );   } }  class MyHandler implements HttpHandler {   public void handle(HttpExchange exchange) throws IOException {     String requestMethod = exchange.getRequestMethod();     if (requestMethod.equalsIgnoreCase("GET")) {       Headers responseHeaders = exchange.getResponseHeaders();       responseHeaders.set("Content-Type", "text/plain");       exchange.sendResponseHeaders(200, 0);        OutputStream responseBody = exchange.getResponseBody();       Headers requestHeaders = exchange.getRequestHeaders();       Set<String> keySet = requestHeaders.keySet();       Iterator<String> iter = keySet.iterator();       while (iter.hasNext()) {         String key = iter.next();         List values = requestHeaders.get(key);         String s = key + " = " + values.toString() + "\n";         responseBody.write(s.getBytes());       }       responseBody.close();     }   } } 

Or you can use Jetty for that purpose. It’s quite lightweight and perfectly fits this purpose.

like image 137
Ivan Dubrov Avatar answered Sep 27 '22 18:09

Ivan Dubrov


You can use jetty as embedded server, its fairly light weight. Other option is check this out for a simple java class to handle http requests http://java.sun.com/developer/technicalArticles/Networking/Webserver/.

Other way is in Java 6 you can use com.sun.net.httpserver.HttpServer

like image 41
Bhushan Bhangale Avatar answered Sep 27 '22 17:09

Bhushan Bhangale


Sun embedded web server is useful, but com.sun.net package could be dropped without notice. A better alternative are

  • http://tjws.sourceforge.net/ 100kb very small and jdk 1.6-aware
  • http://winstone.sourceforge.net/ bigger but a good shot
  • http://www.eclipse.org/jetty/ Jetty, very good in developement, support SPDY and websocket
like image 31
daitangio Avatar answered Sep 27 '22 19:09

daitangio


If you're not using Java 6, then I would certainly recommend Jetty. That works very well and has a decent programming interface.

like image 36
Brian Agnew Avatar answered Sep 27 '22 17:09

Brian Agnew