2

我正在尝试使用此处的示例从 Jetty 7 升级到 Jetty 9:

http://www.eclipse.org/jetty/documentation/current/embedding-jetty.html#d0e18177

Server server = new Server(8080);
ServletContextHandler context=new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
HttpServlet servlet=new TestServlet();
context.addServlet(new ServletHolder(servlet), "/*");
server.setHandler(context);
server.start();
server.join();

但是,它在运行时死亡:

java.lang.NullPointerException
at org.eclipse.jetty.util.component.ContainerLifeCycle.addBean(ContainerLifeCycle.java:304)
at org.eclipse.jetty.util.component.ContainerLifeCycle.addBean(ContainerLifeCycle.java:213)
at org.eclipse.jetty.server.Server.<init>(Server.java:128)
at org.eclipse.jetty.server.Server.<init>(Server.java:103)

示例中的 servlet 是一个简单的 hello world 示例。

4

1 回答 1

3

下面是使用提供的 Jetty 'DefaultServlet' 类使用 Jetty9 Embedded/Servlets 的示例。

public static void main(String[] args) throws Exception {
  Server server = new Server(8080);
  ServletContextHandler context=new ServletContextHandler(ServletContextHandler.SESSIONS);
  context.setContextPath("/");
  DefaultServlet defaultServlet = new DefaultServlet();
  ServletHolder holderPwd = new ServletHolder("default", defaultServlet);
context.addServlet(holderPwd, "/*");
  server.setHandler(context);
  server.start();
  server.join();
}

输出:

2014-04-08 10:04:18.173:INFO::main: 日志记录初始化 @132ms 2014-04-08 10:04:18.243:INFO:oejs.Server:main: jetty-9.1.3.v20140225 2014-04- 08 10:04:18.282:INFO:oejs.ContextHandler:main: 启动 oejsServletContextHandler@52e21a17{/,null,AVAILABLE} 2014-04-08 10:04:18.298:INFO:oejs.ServerConnector:main: 启动 ServerConnector@7388665b{ HTTP/1.1}{0.0.0.0:8080} 2014-04-08 10:04:18.298:INFO:oejs.Server:main: 开始 @259ms

于 2014-03-25T07:56:58.840 回答