2013-11-04 61 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本身使用「/」作爲內容路徑。 –