我有一個應用程序,所以web.xml。在web.xml中我配置瞭如下的錯誤頁面當請求作爲轉發時,錯誤頁面標記不呈現相應的錯誤頁面
<error-page>
<error-code>404</error-code>
<location>/error/err404.jsp</location>
</error-page>
上面的東西對於web.xml中不存在的uri完美起作用。
For Eg:
1. localhost:8080/appName/testServlet (Exists in web.xml)
2. localhost:8080/appName/testServletXXX (Not exists)
兩個網址都很好地工作(第一url呈現實際的頁面,第二網址呈現err404.jsp)時,我直接在瀏覽器中輸入網址。
當我將請求轉發到uri「testServlet」時,它正常工作,因爲它存在於web.xml中。 uri(testServletXXX)在web.xml中不存在時會出現問題。如果URI是不是在web.xml中存在,它不是渲染err404.jsp而是它的渲染錯誤:
HTTP Status 404 - /appName/xxxx
type Status report
message /appName/xxxx
description The requested resource is not available.
Apache Tomcat/7.0.32
這是從Tomcat。
下面是轉發請求的代碼。
ServletContext scRoot = getServletConfig().getServletContext().getContext("/appName");
rd = scRoot.getRequestDispatcher("testServlet");
rd.forward(request, response);
錯誤頁面爲什麼沒有爲web.xml中不存在的uri呈現err404頁面。這在轉發的情況下尤其如此。我無法弄清楚。我錯過任何概念/邏輯嗎?感謝任何幫助。提前致謝。
你能舉一個這樣的URL的例子嗎(是絕對的還是相對的?) – 2014-10-01 09:28:27
感謝您的回覆Serge,現在我編輯了這篇文章。只需檢查它。謝謝。 – venky 2014-10-01 10:06:25
不確定它是否足夠,但ServletContext的javadoc說:*路徑名必須以/開頭,並被解釋爲相對於當前上下文根*:嘗試使用''/ testServlet''。除非你有多個ServletContext,否則'ServletContext scRoot = getServletConfig().getServletContext();'應該足夠了。你應該測試'rd'不是null。 – 2014-10-01 10:16:30