http server

前言

httpserver除了tomcat,jboss等以容器方式存在,也可以在应用中自己编码实现。 常用的有JDK Httpserver,jetty,tomcat embedded。

JDK HttpServer

jdk1.6后,加入了httpserver包,非常轻量级。使用也简单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

public class MyHttpServer {

public static void main(String[] args) {
InetSocketAddress address = new InetSocketAddress(8091);
HttpServer server = null;
try {
server = com.sun.net.httpserver.HttpServer.create(address,0);
} catch (IOException e) {
e.printStackTrace();
}
server.createContext("/server",new MyHttpHandler());
server.setExecutor(Executors.newFixedThreadPool(20));
server.start();
System.out.println("server is listening on port 8091");
}
}


import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import java.io.IOException;
import java.io.OutputStream;

public class MyHttpHandler implements HttpHandler {

@Override
public void handle(HttpExchange httpExchange) throws IOException {
Headers responseHeaders = httpExchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
httpExchange.sendResponseHeaders(200, 0);
OutputStream os = httpExchange.getResponseBody();
os.write("welcome to my blackb's world".getBytes());
os.flush();
os.close();
}
}

小结:
定义sever,包括端口,绑定context和handler

Jetty

  1. 文档丰富、功能强大、资料多。
  2. 可以作为容器使用,也可嵌入到Application
  3. 支持多端口
  4. 支持http2
  5. 支持各种日志插件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    public class HttpServerDemo 
    {
    public static void main( String[] args )
    {

    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(8092);
    server.setConnectors(new Connector[]{connector});
    server.setHandler(new MyHttpHandler());

    try {
    server.start();
    System.out.println("server is listening on port 8092");
    server.join();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    public class MyHttpHandler extends AbstractHandler{

    @Override
    public void handle(String arg0, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
    // Declare response encoding and types
    response.setContentType("text/html; charset=utf-8");

    // Declare response status code
    response.setStatus(HttpServletResponse.SC_OK);

    // Write back response
    response.getWriter().println("<h1>Welcome to blackb's jetty World</h1>");

    // Inform jetty that this request has now been handled
    baseRequest.setHandled(true);
    }

    }

小结:
jetty的使用方式和jdk httpserver差不多,区别在jetty有connettor,start后建议join,不需要context。

Tomcat Embedded

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class TomcatServerDemo {
public static void main(String[] args) {
Tomcat tomcat = new Tomcat();
tomcat.setPort(8093);
Context ctx = tomcat.addContext("/", new File(".").getAbsolutePath());
Tomcat.addServlet(ctx, "Embedded", new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

Writer w = resp.getWriter();
w.write("Welcome to balckb's Embedded Tomcat servlet.\n");
w.flush();
w.close();
}
});

ctx.addServletMapping("/*", "Embedded");
try {
tomcat.start();
} catch (LifecycleException e) {
e.printStackTrace();
}
System.out.println("server is listening on port 8093");
tomcat.getServer().await();
}

}

小结:
tomcat不太一样,需要定义servlet。将路径与servlet绑定。

其他

其他的还有Grizzly HttpServer等。

参考资料

https://blog.csdn.net/qq_405930170/article/details/70770095
jetty博客:https://www.cnblogs.com/yiwangzhibujian/p/5832597.html
jetty官文:https://www.eclipse.org/jetty/documentation/current/
tomcat embedded:http://zetcode.com/web/embeddedtomcat/

扩展

关于restful接口,目前比较流行的实现有以下几种:
Apache CXF,开源的Web服务框架开源组织Apache的实现;Jersey,由Sun提供的JAX-RS的参考实现;RestEasy,JBoss的JAX-RS的实现。