【问题标题】:Empty Context String Warning空上下文字符串警告
【发布时间】: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 框架上。

标签: java jetty


【解决方案1】:

文档是这么说的

上下文路径是用于选择的 URL 路径的前缀 传入请求传递到的上下文。通常是一个 URL 在 Java servlet 服务器中的格式为 http://hostname.com/contextPath/servletPath/pathInfo,其中每个 路径元素可以是零个或多个/分隔元素。 如果有 是没有上下文路径的,上下文被称为根上下文。 根上下文必须配置为“/”,但报告为 servlet API getContextPath() 方法的空字符串。

所以,我猜你可以使用 "/"

http://www.eclipse.org/jetty/documentation/current/configuring-contexts.html

【讨论】:

  • 问题是它以“/”结尾,这是在将路径设置为“/”时引发的抱怨。我想这是某种错误,因为“/”接缝是一种特殊情况。
  • 我刚刚注意到,如果 path.length() == 0,Jetty 本身使用“/”作为内容路径。
【解决方案2】:

在我注意到(感谢@Ozan!)在将上下文路径设置为“”的情况下使用“/”之后,我尝试为此添加错误请求。所以我认为这是一个错误,是的。此问题已存在bug report,它已在 9.0.6 中修复,自 2013 年 9 月 30 日起可用。所以我刚刚升级了码头版本,警告现在消失了。

Jetty 代码现在检查路径的长度是否大于 1:

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.length()>1 && 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();
    }
}

【讨论】:

    猜你喜欢
    • 2014-06-17
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多