首页 > Web开发 > 详细

网站404,500错误页面的处理,及500异常写入errorLog日志

时间:2014-01-19 15:58:27      阅读:564      评论:0      收藏:0      [点我收藏+]

1.web.xml 配置

bubuko.com,布布扣
<error-page>
      <error-code>404</error-code>
      <location>/404.jsp</location>
  </error-page>
  <error-page>
      <error-code>500</error-code>
      <location>/500.jsp</location>
  </error-page>
bubuko.com,布布扣

 

2.定义404.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" isErrorPage="true"%>
<%response.setStatus(HttpServletResponse.SC_OK); %> 

<h1>您所查看的商品或页面没有找到</h1>

 

3.定义500.jsp

bubuko.com,布布扣
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" isErrorPage="true"%>
<%response.setStatus(HttpServletResponse.SC_OK); %>

<h1>很抱歉,您访问的页面出错了!</h1>

<div id="errorMessageDiv" style="display:;">            
<pre>        
<%
try {                        //全部内容先写到内存,然后分别从两个输出流再输出到页面和文件                       
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();                        
    PrintStream printStream = new PrintStream(byteArrayOutputStream);                        
    printStream.println();                       
    
    UserInfoDTO requestUser = (UserInfoDTO)request.getSession().getAttribute("userLogin");
    printStream.println("用户信息");                        
    if(requestUser != null){
        printStream.println("账号:" + requestUser.getNickname());     
    }else{
        printStream.println("账号:游客");    
    }
    printStream.println("访问的路径: " + request.getAttribute("javax.servlet.forward.request_uri"));                        
    printStream.println();              
    
    printStream.println("异常信息");                        
    printStream.println(exception.getClass() + " : " + exception.getMessage());                        
    printStream.println();                        
    
    Enumeration<String> e = request.getParameterNames();                        
    if (e.hasMoreElements()) {                            
        printStream.println("请求中的Parameter包括:");                            
        while (e.hasMoreElements()) {                                
            String key = e.nextElement();                                
            printStream.println(key + "=" + request.getParameter(key));                            
            }                            
        printStream.println();                        
    } 
    
    printStream.println("堆栈信息");                        
    exception.printStackTrace(printStream);                        
    printStream.println();                        
    out.print(byteArrayOutputStream);    //输出到网页                        
    
    Calendar calendar = Calendar.getInstance();
    /**按年月日来分*/
    int year = calendar.get(Calendar.YEAR);//得到年
    int month = calendar.get(Calendar.MONTH)+1;//得到月,因为从0开始的,所以要加1
    int day = calendar.get(Calendar.DAY_OF_MONTH);//得到天
    
    String saveurl = Constants.ROOTPATH + "errorLog/";
    String path1 = saveurl + year + "/" ;
    String path2 = saveurl + year + "/" + month + "/" ;
    String path3 = saveurl + year + "/" + month + "/" + day + "/" ;
    
    //建立按年月日文件夹,如果文件夹不存在,就建立新的文件夹。
    FileOperate.newFolder(path1);
    FileOperate.newFolder(path2);
    FileOperate.newFolder(path3);
    
    //System.err.print("AAAAA"+request.getRealPath("/errorLog"));  //项目的根目录
    //File dir = new File(request.getRealPath("/errorLog"));    
    File dir = new File(path3);    
    //if (!dir.exists()) {                            
    //    dir.mkdir();                        
    //}                        
    
    String timeStamp = new SimpleDateFormat("yyyyMMddHHmmssS").format(new Date());                        
    FileOutputStream fileOutputStream = new FileOutputStream(new File(dir.getAbsolutePath() + File.separatorChar + "error-" + timeStamp + ".txt"));                        
    new PrintStream(fileOutputStream).print(byteArrayOutputStream); //写到文件                   
} catch (Exception ex) {                        
    ex.printStackTrace();                    
}
%>
</pre>        
</div>
bubuko.com,布布扣

网站404,500错误页面的处理,及500异常写入errorLog日志

原文:http://www.cnblogs.com/simpledev/p/3525893.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!