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

发布于 2024-07-29 07:52:08 字数 201 浏览 9 评论 0原文

我希望在我的 Java Swing 应用程序中嵌入一个非常轻量级的 HTTP 服务器,它只接受请求、执行一些操作并返回结果。

是否有一个非常轻量级的 Java 类可以在我的应用程序中使用,它侦听指定端口上的 HTTP 请求并让我处理请求?

请注意,我并不是在寻找独立的 HTTP 服务器,而是在寻找一个可以在我的应用程序中使用的小型 Java 类。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷心人i 2024-08-05 07:52:09

Sun 嵌入式 Web 服务器很有用,但 com.sun.net 软件包可能会被删除,恕不另行通知。
更好的选择是

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

无法回应 2024-08-05 07:52:09

如果您不使用 Java 6,那么我肯定会推荐 Jetty。 它工作得很好并且有一个不错的编程接口。

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

美人如玉 2024-08-05 07:52:09

您说了两次“非常轻”,所以我认为 JLHTTP 可能很适合您。 您可以将其嵌入为单个源文件或 ~35K/50K jar 文件,但它支持开箱即用的 HTTP 服务器中所需的大多数功能。

免责声明:我是作者。 但请亲自检查一下,看看您的想法:-)

You said "very light" twice, so I think JLHTTP might be a good match for you. 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.

Disclaimer: I'm the author. But check it out for yourself and see what you think :-)

从 Java 6 开始,JDK 包含一个简单的 HTTP 服务器 实现。

用法示例:

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();
    }
  }
}

或者您可以使用 Jetty 用于此目的。 它非常轻,非常适合这个目的。

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.

一刻暧昧 2024-08-05 07:52:08

你可以使用jetty作为嵌入式服务器,它的重量相当轻。 其他选项是检查一个简单的 java 类来处理 http 请求 http://java .sun.com/developer/technicalArticles/Networking/Webserver/

另一种方法是在 Java 6 中你可以使用 com.sun.net.httpserver.HttpServer

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文