11

I've got a servlet that I wish to deploy programmatically using Jetty. The servlet uses Spring and it's web.xml points to the Spring context XML file as you'd expect.

At the moment, I'm just trying the example code from the Jetty docs but with my own 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();

This results in the following exception:

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

How can I set up my servlet so that Jetty knows where it's web.xml and Spring context are?

Any help would really be appreciated!

EDIT

So, apparently I don't need a web.xml but I do need to point Jetty to my Spring context. I've tried something like the following:

context.setInitParameter("contextConfigLocation", "classpath*:**/*Context.xml");

But it doesn't work (still produces the same exception). I've also tried setting the "contextConfigLocation" on the ServletHolder to no avail.

DeadPassive
  • 877
  • 3
  • 8
  • 22

2 Answers2

19

For those who are interested, I got this to work as follows:

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();

The key step I was missing was

context.addEventListener(new ContextLoaderListener());

which loads the Spring context.

DeadPassive
  • 877
  • 3
  • 8
  • 22
  • instead of using testContext.xml you could have used `context.setInitParameter("contextConfigLocation", AppConfig.class.getName());` – PC. Jul 31 '13 at 12:47
0

if you want to process a web.xml you need to use a WebappContext, a servlet context doesn't know anything about web.xml's

see: http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/OneWebApp.java

jesse mcconnell
  • 7,102
  • 1
  • 22
  • 33
  • Thanks for the reply. However, I already know how to set it up to use a WAR. My problem is that I need to be able to debug inside the servlet. – DeadPassive May 24 '12 at 15:01
  • if you are setup to use a war then you don't need to use the servlet context like you have in your example above...so its unclear to me what your actually looking for here, a servlet as you have defined _has_ no web.xml that is need know about, perhaps what your looking for is how to get the spring context and push it into your servlet from above.. – jesse mcconnell May 24 '12 at 15:47
  • Again, I can't use a WAR because that would be mean my servlet is compiled and I can't debug within it while it's running. Maybe you're right and the servlet just needs to know about the Spring context. Any idea how I can specify it? – DeadPassive May 25 '12 at 08:21