用 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
Create a simple HTTP server with Java?
提问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
回答by Dan
回答by Michael Borgwardt
回答by ng.
回答by Philippe Signoret
This is how I would go about this:
这就是我将如何解决这个问题:
- Start a
ServerSocket
listening (probably on port 80). - Once you get a connection request, accept and pass to another thread/process (this leaves your
ServerSocket
available to keep listening and accept other connections). - Parse the request text (specifically, the headers where you will see if it is a GET or POST, and the parameters passed.
- Answer with your own headers (
Content-Type
, etc.) and the HTML.
- 开始
ServerSocket
侦听(可能在端口 80)。 - 收到连接请求后,接受并传递给另一个线程/进程(这使您
ServerSocket
可以继续侦听并接受其他连接)。 - 解析请求文本(特别是,您将看到它是 GET 还是 POST 的标头,以及传递的参数。
- 用您自己的标题(
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 服务器。
By the way, if you plan to have a rest web service, here is a simple example using jersey.
回答by nikdeapen
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.
编辑 >> 无法访问上述链接。以下是 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/