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

时间:2010-04-26 22:14:07

标签: java http post get

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

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

13 个答案:

答案 0 :(得分:44)

使用Jetty。这是embedding Jetty的教程。 (这是一个outdated tutorial。)

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

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

答案 1 :(得分:31)

我会这样做:

  1. 开始ServerSocket监听(可能在端口80上)。
  2. 收到连接请求后,接受并传递给另一个线程/进程(这使您的ServerSocket可以继续侦听并接受其他连接。)
  3. 解析请求文本(具体而言,如果是GET或POST,您将看到的标题,以及传递的参数。
  4. 回答您自己的标题(Content-Type等)和HTML。
  5. 我发现使用Firebug(在Firefox中)查看标题示例很有用。这就是你想要模仿的东西。

    试试这个链接:   - Multithreaded Server in Java

答案 2 :(得分:21)

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

答案 3 :(得分:8)

如果您使用的是Sun JDK,则可以使用this built in library
Look at this site on how to use.

如果没有几个Open Source HTTP Servers here可以嵌入到您的软件中。

答案 4 :(得分:4)

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

Check the thread here

顺便说一句,如果您计划使用其他网络服务,则此处为simple example using jersey

答案 5 :(得分:4)

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

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

编辑>> 无法访问上述链接。以下是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/

答案 6 :(得分:3)

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

答案 7 :(得分:1)

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

答案 8 :(得分:1)

嵌入Tomcat是相对轻松的,因为这样的事情。 Here's关于它的一个很好的StackOverflow参考。

答案 9 :(得分:1)

我已经实施了一个link

为了处理json,我使用了杰克逊。如果需要

,也可以将其删除

答案 10 :(得分:0)

servlet容器绝对是最佳选择。如果Tomcat或Jetty对您来说太重了,请考虑WinstoneTTiny

答案 11 :(得分:0)

Undertow是轻量级的无阻塞embedded web server,您可以非常快速地启动并运行。

public static void main(String[] args) {
Undertow.builder()
        .addHttpListener(8080, "localhost")
        .setHandler((exchange) -> exchange.getResponseSender().send("hello world"))
        .build().start();
}

答案 12 :(得分:-1)

我刚刚添加了一个公共仓库,准备好使用Jetty和JDBC运行开箱即用服务器来启动项目。

从github拉出来: https://github.com/waf04/WAF-Simple-JAVA-HTTP-MYSQL-Server.git