我可以向WebAppContext添加多个servlet吗?

时间:2012-11-27 19:07:11

标签: scala servlets jetty scalatra

我有以下Scala代码来设置带Scalatra的Jetty服务器。

val server = new Server(8080)
val context = new WebAppContext()
context.setResourceBase("visualization")
context.addServlet(new ServletHolder(new CallTreeServlet(dataProvider)), "/*")
context.addServlet(new ServletHolder(new DataLoadingServlet(dataProvider)), "/*")
server.setHandler(context)

我的问题是它似乎只在我注册一个servlet时才有用。

如果我注册了多个,就像我在我发布的代码中所做的那样,它只加载其中一个。

是否可以加载多个servlet?我想是的,但我无法弄明白。

如果我尝试从第一个servlet加载页面,我收到此错误消息,该消息仅引用属于第二个servlet的页面:

Requesting "GET /callTrees" on servlet "" but only have:
GET /components
POST /load
POST /searchCallTrees
POST /selectPlugIn

1 个答案:

答案 0 :(得分:2)

要解决此问题,您应该验证servlet生命周期。一种方便的方法是仔细阅读servlet容器的日志,以查看启动Web应用程序时报告的内容。它应该告诉您每个Web应用程序(servlet上下文)和每个servlet。 。

但是,我想我知道你的问题是什么。您的servlet路径映射有点时髦。在我看来,您正在映射两个servlet以接收所有请求。从实际的角度来看,这不起作用,并且可能无法在servlet规则方面起作用。从servlet规范:

SRV.11.2
Specification of Mappings
In the Web application deployment descriptor, the following syntax is used to define
mappings:
• A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used
for path mapping.
• A string beginning with a ‘*.’ prefix is used as an extension mapping.
• A string containing only the ’/’ character indicates the "default" servlet of
the application. In this case the servlet path is the request URI minus the con-
text path and the path info is null.
• All other strings are used for exact matches only.

我建议你让它们都是独一无二的。现在看来,你把它们都放在“/ *”里,这有点像“默认的servlet”,但不是。 。 。

为什么不尝试“/ first / ”和“/ second / ”作为完整性检查。然后从那里移动到获得你喜欢的配置。