網站404,500錯誤頁面的處理,及500異常寫入errorLog日志


1.web.xml 配置

<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>

 

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

<%@ 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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM