JAVA WEB项目异常处理提示页
2016-04-28 16:11
627 查看
最近一直感到困惑,为啥我的后台报错就赤果果的将程序中的错误打印在页面,这样在项目上线后,如果遇到500,java.lang.Exception等错误时,会提示很尴尬的信息
感谢http://blog.csdn.net/zhangxin09/article/details/50644694
1.web.xml中配置
2.在util页面中COPY
http://blog.csdn.net/zhangxin09/article/details/50644694 中的写法如下
3.在error_500.jsp中引入
到此完结,看效果
一定注意<%@ page language="java" pageEncoding="UTF-8" isErrorPage="true"%>将isErrorPage="true"设置为TRUE
感谢http://blog.csdn.net/zhangxin09/article/details/50644694
1.web.xml中配置
<error-page> <exception-type>java.lang.Exception</exception-type> <location>/error/error_500.jsp</location> </error-page> <error-page> <error-code>403</error-code> <location>/error/error_404.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/error/error_404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/error/error_500.jsp</location> </error-page> <error-page> <error-code>503</error-code> <location>/error/error_500.jsp</location> </error-page> <error-page> <exception-type>java.lang.NullPointException</exception-type> <location>/error/error_500.jsp</location> </error-page>
2.在util页面中COPY
http://blog.csdn.net/zhangxin09/article/details/50644694 中的写法如下
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.io.*"%> <%@page import="java.util.*"%> <%! class ErrorHandler { // 全部内容先写到内存,然后分别从两个输出流再输出到页面和文件 private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); private PrintStream printStream = new PrintStream(byteArrayOutputStream); /** * 收集错误信息 * @param request * @param exception * @param out */ public ErrorHandler(HttpServletRequest request, Throwable exception, JspWriter out) { setRequest(request); setException(exception); if(out != null) { try { out.print(byteArrayOutputStream); // 输出到网页 } catch (IOException e) { e.printStackTrace(); } } log(request); if(byteArrayOutputStream != null) try { byteArrayOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } if(printStream != null) printStream.close(); } /** * * @param request */ private void setRequest(HttpServletRequest request) { HttpSession session = request.getSession(); String username=(String)session.getAttribute("edu.yale.its.tp.cas.client.filter.user"); printStream.println(); printStream.println("用户账号:" + username); // printStream.println("用户账号:" + request.getSession().getAttribute("userName")); printStream.println("访问的路径: " + getInfo(request, "javax.servlet.forward.request_uri", String.class)); printStream.println("出错页面地址: " + getInfo(request, "javax.servlet.error.request_uri", String.class)); printStream.println("错误代码: " + getInfo(request, "javax.servlet.error.status_code", int.class)); printStream.println("异常的类型: " + getInfo(request, "javax.servlet.error.exception_type", Class.class)); printStream.println("异常的信息: " + getInfo(request, "javax.servlet.error.message", String.class)); printStream.println("异常servlet: " + getInfo(request, "javax.servlet.error.servlet_name", String.class)); printStream.println(); // 另外两个对象 getInfo(request, "javax.servlet.jspException", Throwable.class); getInfo(request, "javax.servlet.forward.jspException", Throwable.class); Map<String, String[]> map = request.getParameterMap(); for (String key : map.keySet()) { printStream.println("请求中的 Parameter 包括:"); printStream.println(key + "=" + request.getParameter(key)); printStream.println(); } for (Cookie cookie : request.getCookies()){ // cookie.getValue() printStream.println("请求中的 Cookie 包括:"); printStream.println(cookie.getName() + "=" + cookie.getValue()); printStream.println(); } } /** * * @param exception */ private void setException(Throwable exception) { if (exception != null) { printStream.println("异常信息"); printStream.println(exception.getClass() + " : " + exception.getMessage()); printStream.println(); printStream.println("堆栈信息"); exception.printStackTrace(printStream); printStream.println(); } } /** * * @param request */ private void log(HttpServletRequest request) { File dir = new File(request.getSession().getServletContext().getRealPath("/errorLog")); if (!dir.exists()) { dir.mkdir(); } String timeStamp = new java.text.SimpleDateFormat("yyyyMMddhhmmssS").format(new Date()); File file = new File(dir.getAbsolutePath() + File.separatorChar + "error-" + timeStamp + ".txt"); } /** * * @param request * @param key * @param type * @return */ @SuppressWarnings("unchecked") private <T> T getInfo(HttpServletRequest request, String key, Class<T> type){ Object obj = request.getAttribute(key); return obj == null ? null : (T) obj; } } %>
3.在error_500.jsp中引入
<%@ page language="java" pageEncoding="UTF-8" isErrorPage="true"%> <%@ page import="java.io.PrintStream" %> <%@ include file="../error/util.jsp"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path; if (!path.endsWith("/")) { basePath = basePath + "/"; } %> <!doctype html> <html> <head> <meta charset="UTF-8"> <title>新版BMS支撑系统</title> <link rel="stylesheet" type="text/css" href="<%=basePath%>css/common.css"/> <link rel="stylesheet" type="text/css" href="<%=basePath%>css/main.css"/> <style> textarea { width: 100%; min-height: 300px; } </style> <script type="text/javascript" src="<%=basePath%>js/libs/modernizr.min.js"></script> </head> <body> <div class="crumb-wrap"> <div class="crumb-list"><i class="icon-font"></i><a href="#">首页</a><span class="crumb-step">></span><span class="crumb-name">系统错误提示505</span></div> </div> <div class="result-wrap"> <div class="config-items"> <div class="result-content"> <table width="100%" align="center" class="insert-tab"> <tbody> <tr> <td><img src="images/icon_cry.png"/><br/> <h3>500或者503或者java.lang.NullPointException或者java.lang.Exception</h3> <span style="font-size:14px;color:red;">亲爱的同事:<br/> 您好!<br/> 我们致力打造更好的运营后台,但人算不如天算,有些错误发生了,希望是在控制范围内的...<br/> 如果错误重复出现,请向咱们的码农同事反馈。<br/> 错误信息如下:<br/></span> </td> </tr> <tr> <td> <textarea> <% new ErrorHandler(request, exception, out); %> </textarea> </td> </tr> <tr> <td> <input type="button" value="返回" onclick="history.go(-1)" class="btn btn6"> </td> </tr> </tbody></table> </div> </div> </div> </body> </html>
到此完结,看效果
一定注意<%@ page language="java" pageEncoding="UTF-8" isErrorPage="true"%>将isErrorPage="true"设置为TRUE
相关文章推荐
- 设计模式-单例模式
- java发短信
- java反射机制详解 及 Method.invoke解释
- 如何搭建jdk环境
- 关于java的反射机制及动态代理
- ubuntu之安装java浏览器插件
- eclipse 高效快捷键大全
- 【排序算法】归并排序(java实现)
- cmd执行Java源码需要注意的一些地方
- 深入理解Java之线程池(下)
- 深入理解Java之线程池(中)
- java 集合----Map、Collection
- 深入理解Java之线程池(上)
- SpringMVC 过滤参数的非法字符
- 多数据源的配置
- Java Web的数据库操作
- java观察者模式
- eclipse, Log4j配置(真心的详细~)
- 怎么在MySQL官网下载java连接MySQL数据库的驱动jar包
- java操作sqlserver