在maven中添加依赖
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.1.4.v20140401</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.1.4.v20140401</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
<version>9.1.4.v20140401</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>9.1.4.v20140401</version>
</dependency>
运行示例代码
public class Main {
public static class HelloServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.getWriter().println("hello world");
}
}
public static void main( String[] arg) throws Exception {
Server server = new Server(8111);
ServletContextHandler servletContextHandler = new ServletContextHandler();
servletContextHandler.setContextPath("/");
servletContextHandler.addServlet(HelloServlet.class, "/hello");
HandlerList handlerList = new HandlerList();
handlerList.setHandlers(new Handler[]{servletContextHandler});
server.setHandler(handlerList);
server.start();
server.join();
}
}
网友评论