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

 

你可能感兴趣的:(error)