简体   繁体   English

Swing应用程序中嵌入式HTTP服务器的Java类

[英]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. 我希望在我的Java Swing应用程序中嵌入一个非常轻的HTTP服务器,它只接受请求,执行一些操作并返回结果。

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? 是否有一个非常轻的Java类,我可以在我的应用程序中使用它在指定的端口上侦听HTTP请求并让我处理请求?

Note, that I am not looking for a stand-alone HTTP server, just a small Java class which I can use in my app. 请注意,我不是在寻找一个独立的HTTP服务器,只是一个可以在我的应用程序中使用的小型Java类。

Since Java 6, the JDK contains a simple HTTP server implementation. 从Java 6开始,JDK包含一个简单的HTTP服务器实现。

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. 或者您可以将Jetty用于此目的。 It's quite lightweight and perfectly fits this purpose. 它非常轻巧,非常适合这个目的。

You can use jetty as embedded server, its fairly light weight. 你可以使用jetty作为嵌入式服务器,它的重量相当轻。 Other option is check this out for a simple java class to handle http requests http://java.sun.com/developer/technicalArticles/Networking/Webserver/ . 其他选项是检查一个简单的java类来处理http请求http://java.sun.com/developer/technicalArticles/Networking/Webserver/

Other way is in Java 6 you can use com.sun.net.httpserver.HttpServer 其他方式是在Java 6中你可以使用com.sun.net.httpserver.HttpServer

Sun embedded web server is useful, but com.sun.net package could be dropped without notice. Sun嵌入式Web服务器很有用,但com.sun.net包可能会被丢弃,恕不另行通知。 A better alternative are 一个更好的选择是

If you're not using Java 6, then I would certainly recommend Jetty . 如果您不使用Java 6,那么我肯定会推荐Jetty That works very well and has a decent programming interface. 这非常有效并且具有良好的编程接口。

You said "very light" twice, so I think JLHTTP might be a good match for you. 你说“非常轻”两次,所以我认为JLHTTP可能是你的最佳选择。 You can embed it as a single source file or a ~35K/50K jar file, yet it supports most functionality you'd need in an HTTP server out of the box. 您可以将其嵌入为单个源文件或~35K / 50K jar文件,但它支持您在HTTP服务器中开箱即用所需的大多数功能。

Disclaimer: I'm the author. 免责声明:我是作者。 But check it out for yourself and see what you think :-) 但请亲自检查一下,看看你的想法:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM