我有一个servlet,我希望使用Jetty以编程方式部署。 servlet使用Spring,它的web.xml指向Spring上下文XML文件,正如您所期望的那样。
目前,我只是尝试Jetty文档中的示例代码,但使用自己的servlet:
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(new BatchReceiver()),"/br/*");
server.start();
server.join();
这会导致以下异常:
2012-05-24 14:43:20.190:INFO:oejs.Server:jetty-8.1.3.v20120416
2012-05-24 14:43:20.266:WARN:/:unavailable
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:84)
at com.spiffymap.sealog.server.servlet.BatchReceiver.init(BatchReceiver.java:126)
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:492)
at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:312)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:778)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:258)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:699)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:90)
at org.eclipse.jetty.server.Server.doStart(Server.java:262)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at com.spiffymap.sealog.server.servlet.TestBatchReceiver.main(TestBatchReceiver.java:26)
2012-05-24 14:43:20.335:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
如何设置我的servlet以便Jetty知道它的web.xml和Spring上下文在哪里?
真的很感激任何帮助!
修改
所以,显然我不需要web.xml但我做需要将Jetty指向我的Spring上下文。我尝试过以下内容:
context.setInitParameter("contextConfigLocation", "classpath*:**/*Context.xml");
但它不起作用(仍产生相同的异常)。我也尝试在ServletHolder上设置“contextConfigLocation”无济于事。
答案 0 :(得分:17)
对于那些感兴趣的人,我按照以下方式开展工作:
Server server = new Server(8090);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/batch");
// Setup Spring context
context.addEventListener(new ContextLoaderListener());
context.setInitParameter("contextConfigLocation", "classpath*:**/testContext.xml");
server.setHandler(context);
// Add servlets
context.addServlet(new ServletHolder(new BatchReceiver()), "/receiver/*");
context.addServlet(new ServletHolder(new BatchSender()), "/sender/*");
server.start();
server.join();
我缺少的关键步骤是
context.addEventListener(new ContextLoaderListener());
加载Spring上下文。
答案 1 :(得分:0)
如果你想处理web.xml你需要使用WebappContext,那么servlet上下文对web.xml的知识一无所知