如何在web.xml中指定默認(rèn)錯誤頁面?我<error-page>在web.xml中使用元素來指定用戶遇到某個錯誤時的友好錯誤頁面,例如代碼為404的錯誤:<error-page>
<error-code>404</error-code>
<location>/Error404.html</location></error-page>但是,我希望如果用戶不符合指定的任何錯誤代碼<error-page>,他或她應(yīng)該看到默認(rèn)錯誤頁面。如何使用web.xml中的元素執(zhí)行此操作?
3 回答

拉莫斯之舞
TA貢獻(xiàn)1820條經(jīng)驗 獲得超10個贊
在Servlet 3.0或更高版本上,您可以指定
<web-app ...> <error-page> <location>/general-error.html</location> </error-page></web-app>
但是當(dāng)你仍然使用Servlet 2.5時,除了單獨指定每個常見的HTTP錯誤之外別無他法。您需要確定最終用戶可能面臨的HTTP錯誤。在一個準(zhǔn)系統(tǒng)webapp上,例如使用HTTP身份驗證,具有禁用的目錄列表,使用自定義servlet和代碼可能會拋出未處理的異常或者沒有實現(xiàn)所有方法,那么你想為HTTP錯誤設(shè)置它401分別為403,40和503。
<error-page> <!-- Missing login --> <error-code>401</error-code> <location>/general-error.html</location></error-page><error-page> <!-- Forbidden directory listing --> <error-code>403</error-code> <location>/general-error.html</location></error-page><error-page> <!-- Missing resource --> <error-code>404</error-code> <location>/Error404.html</location></error-page><error-page> <!-- Uncaught exception --> <error-code>500</error-code> <location>/general-error.html</location></error-page><error-page> <!-- Unsupported servlet method --> <error-code>503</error-code> <location>/general-error.html</location></error-page>
這應(yīng)該包括最常見的。

Helenr
TA貢獻(xiàn)1780條經(jīng)驗 獲得超4個贊
你也可以這樣做:
<error-page> <error-code>403</error-code> <location>/403.html</location></error-page><error-page> <location>/error.html</location></error-page>
對于錯誤代碼403,它將返回頁面403.html,對于任何其他錯誤代碼,它將返回頁面error.html。

弒天下
TA貢獻(xiàn)1818條經(jīng)驗 獲得超8個贊
您還可以<error-page>
使用<exception-type>
例如以下內(nèi)容指定例外:
<error-page> <exception-type>java.lang.Exception</exception-type> <location>/errorpages/exception.html</location></error-page>
或使用<error-code>
以下內(nèi)容映射錯誤代碼:
<error-page> <error-code>404</error-code> <location>/errorpages/404error.html</location></error-page>
添加回答
舉報
0/150
提交
取消