Java Web程序中error页面解决
发布时间:2021-11-24 13:43:19 所属栏目:教程 来源:互联网
导读:01)web.xml配置 定义错误页面的位置,按错误码不同定位到不同的错误展示页面,系统中分为两类错误,第一类是404页面不存在的错误,另一类是服务器内部错误50x,对应的页面分别为404.jsp和error.jsp error-page error-code500/error-code location/error.jsp/l
01)web.xml配置 定义错误页面的位置,按错误码不同定位到不同的错误展示页面,系统中分为两类错误,第一类是404页面不存在的错误,另一类是服务器内部错误50x,对应的页面分别为404.jsp和error.jsp <error-page> <error-code>500</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> <error-page> <error-code>403</error-code> <location>/error.jsp</location> </error-page> 02)错误处理页面 404.jsp--友好提示用户页面不存在,这个页面没什么特殊,核心是再该页面上给用户提示文案即可 <%@ page contentType="text/html; charset=UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>异常信息</title> <style type="text/css"> body{ font-size:12px; color:#4d4b4b;} </style> </head> <body> <br><br><br><br><br><br> <table width="700" border="1" cellpadding="0" cellspacing="0" align="center"> <tr> <td colspan="2" valign="top" bgcolor="#D6E2F2" class="style01 tdpadding"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="25" class="img01 titlefalse">请求链接错误</td> </tr> <tr> <td height="119" bgcolor="#FFFFFF" class="style01"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="20%" height="24" class="title01" align="center"> 您访问的页面不存在。 </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </body> </html> error.jsp--–对用户友好提示,将错误信息隐藏在页面中,可以通过CTRL+SHIFT+D键查看 <%@ page contentType="text/html; charset=UTF-8"%> <%!String exceptionMsgForInner(Throwable e){ String ls_ErrMsg = e.getLocalizedMessage(); if (ls_ErrMsg == null) ls_ErrMsg = ""; ls_ErrMsg += "rn"; Throwable eCause = e.getCause(); if (eCause == null){ for (int ii = 0;ii < e.getStackTrace().length;ii++){ ls_ErrMsg += e.getStackTrace()[ii].toString() + "rn"; } } else{ ls_ErrMsg += exceptionMsgForInner(eCause); } return ls_ErrMsg.trim(); } %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>异常信息</title> <style type="text/css"> body{ font-size:12px; color:#4d4b4b;} </style> <SCRIPT LANGUAGE="JavaScript"> <!-- function dokeydown(){ if (event.ctrlKey && event.shiftKey && event.keyCode==68){ var obj=document.getElementById("divexception"); if (obj.style.display.toLowerCase()=="none"){ obj.style.display="block"; }else{ obj.style.display="none"; } return false; } } window.document.attachEvent('onkeydown', dokeydown); //--> </SCRIPT> </head> <body> <br><br><br><br><br><br> <table width="700" border="1" cellpadding="0" cellspacing="0" align="center"> <tr> <td colspan="2" valign="top" bgcolor="#D6E2F2" class="style01 tdpadding"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="25" class="img01 titlefalse">异常信息</td> </tr> <tr> <td height="119" bgcolor="#FFFFFF" class="style01"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="20%" height="24" class="title01" align="center"> 系统异常,请联系管理员。 </td> </tr> </table> </td> </tr> <tr><td> <div align=center style="display:none;height:60%" id=divexception> <TEXTAREA NAME="" ROWS="8" COLS="100"> <% Exception ex = (Exception) request.getAttribute("javax.servlet.error.exception"); out.println(exceptionMsgForInner(ex)); %> </TEXTAREA> <div> </td></tr> </table> </td> </tr> </table> </body> </html> 03)struts2中记录异常堆栈到日志文件中 在struts.xml中添加exception拦截器,并且将日志的log功能打开,设置为ERROR,这样在系统出现异常时,利用struts2的拦截器可以把异常详细堆栈信息记录到Log文件中,然后再将页面跳转到我们定义好的出错页面给用户提示 <interceptor-stack name="defaultStack"> <interceptor-ref name="exception"> <param name="logEnabled">true</param> <param name="logLevel">ERROR</param> </interceptor-ref> ![]() (编辑:葫芦岛站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐