JSP捕獲404、500等錯誤頁面並自定義
阿新 • • 發佈:2018-12-17
當網站頁面找不到或者伺服器內部出現錯誤的時候,我們不想讓使用者看到預設的那張 404,500 的錯誤頁面,在web.xml檔案中加入下面程式碼就可以自定義錯誤頁面了
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
自定義404錯誤頁面:
<%@ page contentType="text/html;charset=UTF-8" %> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>404 NotFound</title> <meta name="Keywords" content="404 NotFound"/> <meta name="Description" content="404 NotFound"/> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/> <link href="css/style_404.css" rel="stylesheet" type="text/css"/> <script type="text/javascript"> setTimeout(function () { top.location = '${pageContext.request.contextPath}'; }, 5000) </script> </head> <body> <div class="error404"> <div class="info"> <h1>404</h1> <h2>抱歉,您訪問的頁面不存在或已被刪除!</h2> <p class="p1">5秒後將帶您返回首頁...</p> <a href="index.jsp" class="btn">返回首頁</a> <a href="index.jsp" class="btn btn-brown">返回上一步</a> </div> <div class="pic"> <img src="images/404.gif" alt=""/> </div> </div> </body> </html>
效果如下: