用 Java 创建一个简单的 HTTP 服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2717294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:03:20  来源:igfitidea点击:

Create a simple HTTP server with Java?

javahttppostget

提问by Stefan Kendall

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.

使用 Java 创建简单 HTTP 服务器的最简单方法是什么?是否有任何公共图书馆可以促进这一点?我只需要响应GET/POST,我不能使用应用程序服务器。

What's the easiest way to accomplish this?

实现这一目标的最简单方法是什么?

采纳答案by Kris

Use Jetty. Here's a tutorial for embedding Jetty. (Here's an outdated tutorial.)

使用码头。这是嵌入 Jetty的教程。(这是一个过时的教程。)

Jetty is pretty lightweight, but it does provide a servlet container, which may contradict your requirement against using an "application server".

Jetty 非常轻量级,但它确实提供了一个 servlet 容器,这可能与您对使用“应用程序服务器”的要求相矛盾。

You can embed the Jetty server into your application. Jetty allows EITHER embedded OR servlet container options.

您可以将 Jetty 服务器嵌入到您的应用程序中。Jetty 允许嵌入式或 servlet 容器选项。

回答by Romain Hippeau

If you are using the Sun JDK you can use this built in library
Look at this site on how to use.

如果您使用的是 Sun JDK,您可以使用这个内置库
查看如何使用这个站点。

If n ot there are several Open Source HTTP Servers herewhich you can embed into your software.

如果这里没有几个开源 HTTP 服务器,您可以将它们嵌入到您的软件中。

回答by maerics

Jettyis 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.

Jetty是一种轻松嵌入 HTTP 服务器的好方法。它支持它自己的简单方法来附加处理程序,并且如果您需要更多功能,它是一个完整的 J2EE 应用程序服务器。

回答by Dan

Embedding Tomcat is relatively painless as such things go. Here'sa good StackOverflow reference about it.

在这种情况下,嵌入 Tomcat 相对轻松。这是关于它一个很好的 StackOverflow 参考。

回答by Michael Borgwardt

A servlet container is definitely the way to go. If Tomcat or Jetty are too heavyweight for you, consider Winstoneor TTiny.

servlet 容器绝对是要走的路。如果 Tomcat 或 Jetty 对您来说太重,请考虑WinstoneTTiny

回答by ng.

The easiest is Simplethere is a tutorial, no WEB-INF not Servlet API no dependencies. Just a simple lightweight HTTP server in a single JAR.

最简单的是Simple有一个教程,没有 WEB-INF 没有 Servlet API 没有依赖项。只是一个简单的轻量级 HTTP 服务器在单个 JAR 中。

回答by Philippe Signoret

This is how I would go about this:

这就是我将如何解决这个问题:

  1. Start a ServerSocketlistening (probably on port 80).
  2. Once you get a connection request, accept and pass to another thread/process (this leaves your ServerSocketavailable to keep listening and accept other connections).
  3. Parse the request text (specifically, the headers where you will see if it is a GET or POST, and the parameters passed.
  4. Answer with your own headers (Content-Type, etc.) and the HTML.
  1. 开始ServerSocket侦听(可能在端口 80)。
  2. 收到连接请求后,接受并传递给另一个线程/进程(这使您ServerSocket可以继续侦听并接受其他连接)。
  3. 解析请求文本(特别是,您将看到它是 GET 还是 POST 的标头,以及传递的参数。
  4. 用您自己的标题(Content-Type等)和 HTML回答。

I find it useful to use Firebug (in Firefox) to see examples of headers. This is what you want to emulate.

我发现使用 Firebug(在 Firefox 中)查看标题示例很有用。这就是你想要效仿的。

Try this link: - Multithreaded Server in Java

试试这个链接: - Java 中的多线程服务器

回答by Charles Dobson

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

我写了一个教程,解释了如何用 Java 编写一个简单的 HTTP 服务器。解释代码在做什么以及为什么随着教程的进行服务器是这样编写的。可能有用http://kcd.sytes.net/articles/simple_web_server.php

回答by Titu

Java 6 has a default embedded http server.

Java 6 有一个默认的嵌入式 http 服务器。

Check the thread here

检查这里的线程

By the way, if you plan to have a rest web service, here is a simple example using jersey.

顺便说一句,如果您打算拥有一个休息网络服务,这里是一个使用 jersey简单示例

回答by nikdeapen

I'm suprised this example is'nt here:

我很惊讶这个例子不在这里:

http://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/ElementalHttpServer.java

http://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/ElementalHttpServer.java

EDIT >> The above link is not reachable. Here is an excerpt from the POST example followed by the link to the HTTP examples.

编辑 >> 无法访问上述链接。以下是 POST 示例的摘录,后跟 HTTP 示例的链接。

 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/

http://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/