2010-06-02 31 views
12

我試圖用如何從web.xml捕獲所有錯誤到同一頁面?

<error-page> 
    <exception-type>java.lang.Exception</exception-type> 
    <location>/errors/error.jsp</location> 
</error-page> 

但我這麼想的趕上404錯誤。我怎樣才能捕捉404等錯誤到同一頁面?我想要捕獲所有錯誤代碼到相同的錯誤頁面jsp。

回答

8

您可以添加<error-code>標記爲

<error-page> 
    <error-code>404</error-code> 
    <location>/errors/error.jsp</location> 
</error-page> 

UPDATE:

根據你更新的問題 - 你必須在web.xml中單獨定義每個錯誤代碼。

使用<exception-type>java.lang.Throwable</exception-type> 將捕獲的錯誤500S但不是404

+2

我知道,但我想抓住所有的錯誤代碼到同一頁 – newbie 2010-06-02 08:55:28

+2

@newbie - 讓你增加對每次和每一個錯誤代碼,它們都指向同一頁。 – 2010-06-02 10:07:37

+0

@StephenC:沒有相同的通配符功能嗎? :/ – 2013-11-01 07:48:11

0

在Tomcat7(可能工作在較舊的版本,但我沒有檢查)

加入你想要的錯誤頁面(例如404.jsp, general_error.php等)

web.xml中添加(所有,然後再具體使其適應你的課程代碼):

<error-page> 
    <location>general_error.php</location> 
</error-page> 

<error-page> 
    <error-code>404</error-code> 
    <location>404.jsp</location> 
</error-page> 

<error-page> 
    <error-code>409</error-code> 
    <location>error_page.jsp?error=custom_message</location> 
</error-page> 
1

我用這:

<error-page> 
    <exception-type>java.lang.Exception</exception-type> 
    <location>/pages/exception/sorry.html</location> 
</error-page> 

<error-page> 
    <error-code>403</error-code> 
    <location>/security/403</location> 
</error-page> 

<error-page> 
    <error-code>404</error-code> 
    <location>/security/404</location> 
</error-page> 
相關問題