gpt4 book ai didi

java - 使用 ServletContext 嵌入 Jetty 9

转载 作者:行者123 更新时间:2023-11-30 08:24:51 24 4
gpt4 key购买 nike

我正在尝试使用此处的示例从 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 示例。

最佳答案

下面是一个使用 Jetty9 Embedded/Servlet 的示例,使用提供的 Jetty“DefaultServlet”类。

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: Logging initialized @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:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@52e21a17{/,null,AVAILABLE} 2014-04-08 10:04:18.298:INFO:oejs.ServerConnector:main: Started ServerConnector@7388665b{HTTP/1.1}{0.0.0.0:8080} 2014-04-08 10:04:18.298:INFO:oejs.Server:main: Started @259ms

关于java - 使用 ServletContext 嵌入 Jetty 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22624152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com