【发布时间】:2013-11-15 02:50:04
【问题描述】:
我尝试配置一个码头上下文(以编程方式)以使用服务于根上下文的 servlet。
对于我设置的上下文路径“/”和 servlet 映射“/*”。这完全按照我想要的方式工作,但 Jetty 抱怨(警告)上下文路径以“/”结尾。当我将上下文路径设置为“”(空字符串)时,会导致有关空字符串的警告。
关于这个问题的documentation section of Jetty 指出:
注意
Java Servlet 规范 2.5 不鼓励空的上下文路径字符串,而 Java Servlet 规范 3.0 有效地禁止它。
Jetty源的部分是:
public void setContextPath(String contextPath)
{
if (contextPath == null)
throw new IllegalArgumentException("null contextPath");
if (contextPath.endsWith("/*"))
{
LOG.warn(this+" contextPath ends with /*");
contextPath=contextPath.substring(0,contextPath.length()-2);
}
else if (contextPath.endsWith("/"))
{
LOG.warn(this+" contextPath ends with /");
contextPath=contextPath.substring(0,contextPath.length()-1);
}
if (contextPath.length()==0)
{
LOG.warn("Empty contextPath");
contextPath="/";
}
_contextPath = contextPath;
if (getServer() != null && (getServer().isStarting() || getServer().isStarted()))
{
Handler[] contextCollections = getServer().getChildHandlersByClass(ContextHandlerCollection.class);
for (int h = 0; contextCollections != null && h < contextCollections.length; h++)
((ContextHandlerCollection)contextCollections[h]).mapContexts();
}
}
所以问题是,为了映射到上下文的根,我应该设置什么上下文路径。目前一切正常,但是根据规范或 Jetty 警告设置了禁止的上下文路径,我想我需要一些不同的东西。
【问题讨论】:
-
风格不好:)
-
不,自 1998 年以来,我有 15 年的 Java 经验 :)。
-
但它是一个用于测试目的的嵌入式 Jetty。每次测试都会出现这个警告,因为我停止并重新启动服务器(顺便说一下需要 15 毫秒)。因此,检查日志中的错误并每次都看到此警告对我来说很烦人...... .
-
好吧,我和你一样长,但我没有问题忽略我知道是非必要的警告。一定是个性问题。
-
@MartinKersten 我遇到了一些问题。它显示 "java.lang.IllegalArgumentException: null contextPath" 。你能帮忙吗?我无法找到我在哪里做错了。我的应用程序位于带有码头服务器的 dropwizard 框架上。