jetty

Jetty

(一) HTTP 服务器

1. 管理 Session

2. 代码启动

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
connector = new SelectChannelConnector();
connector.setPort(port);

ServletHandler handler = new ServletHandler();

// 资源过滤
String resources = ConfigUtils.getProperty(JETTY_DIRECTORY);
if (resources != null && resources.length() > 0) {
FilterHolder resourceHolder = handler.addFilterWithMapping(ResourceFilter.class, "/*", Handler.DEFAULT);
resourceHolder.setInitParameter("resources", resources);
}

// 添加 Servlet
ServletHolder pageHolder = handler.addServletWithMapping(PageServlet.class, "/*");
pageHolder.setInitParameter("pages", ConfigUtils.getProperty(JETTY_PAGES));
pageHolder.setInitOrder(2);

Server server = new Server();
server.addConnector(connector);
server.addHandler(handler);
try {
server.start();
} catch (Exception e) {
throw new IllegalStateException("Failed to start jetty server on " + NetUtils.getLocalHost() + ":" + port + ", cause: " + e.getMessage(), e);
}

停止服务器:

1
2
3
4
5
6
7
8
try {
if (connector != null) {
connector.close();
connector = null;
}
} catch (Throwable e) {
logger.error(e.getMessage(), e);
}

推荐文章