2013-11-04 57 views
1

我尝试配置jetty上下文(以编程方式)使用服务根上下文的servlet。空上下文字符串警告

对于上下文路径,我设置“/”和servlet映射“/ *”。这完全按照我想要的方式工作,但Jetty抱怨(警告)以'/'结尾的上下文路径。当我将上下文路径设置为“”(空字符串)时,会产生一个关于空字符串的警告。

documentation section of Jetty有关此问题的状态:

注意
Java Servlet规范2.5阻碍空上下文路径字符串,以及Java Servlet规范3.0有效地禁止它。

防波堤源的部分是:

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警告禁止上下文路径设置,我想我需要不同的东西。

+0

忽略警告? – Kayaman

+0

会是不好的风格:) –

+0

你必须是新的业务。 – Kayaman

回答

1

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

码头代码现在检查如果路径的长度大于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(); 
    } 
} 
+1

欢迎光临:) –

1

该文档说

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

所以,我想你是用细“/”

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

+0

问题是以'/'结尾的是将路径设置为“/”时引发的抱怨。我想这是一种错误,因为'/'接缝是一种特殊情况。 –

+0

我刚刚注意到,如果path.length()== 0,Jetty本身使用“/”作为内容路径。 –