-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathJetty-Servlet.java
More file actions
48 lines (41 loc) · 1.32 KB
/
Copy pathJetty-Servlet.java
File metadata and controls
48 lines (41 loc) · 1.32 KB
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
45
46
47
48
1,依赖
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
</dependencies>
2,代码
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import com.feather.web.servlet.HelloServlet;
public class Application {
public static void main(String[] args) {
//创建服务器,监听端口
Server server = new Server(8000);
//创建 ServletContextHandler
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
//添加Servlet.class 到 ServletContextHandler
servletContextHandler.addServlet(HelloServlet.class, "/hello");
//添加 ServletContextHandler 到 server
server.setHandler(servletContextHandler);
try {
server.start(); //启动服务
server.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}