Create a simple HTTP server with Java?

后端 未结 13 2470
时光取名叫无心
时光取名叫无心 2020-11-28 04:16

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

相关标签:
13条回答
  • 2020-11-28 04:20

    Java 6 has a default embedded http server.

    Check the thread here

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

    0 讨论(0)
  • 2020-11-28 04:21

    Undertow is a lightweight non-blocking embedded web server that you can get up and running very quickly.

    public static void main(String[] args) {
    Undertow.builder()
            .addHttpListener(8080, "localhost")
            .setHandler((exchange) -> exchange.getResponseSender().send("hello world"))
            .build().start();
    }
    
    0 讨论(0)
  • 2020-11-28 04:24

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

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

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

    0 讨论(0)
  • 2020-11-28 04:33

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

    0 讨论(0)
  • 2020-11-28 04:35

    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

    0 讨论(0)
  • 2020-11-28 04:35

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

    0 讨论(0)
提交回复
热议问题