java jsp公共异常页面_java web 自定义错误页面 完整jsp错误页面代码(同时写错误日志) error.jsp...

本文介绍了如何在Java Web应用中设置公共异常页面,通过配置web.xml将错误重定向到error.jsp,并展示了一个完整的jsp错误页面代码示例。该页面不仅显示错误信息,还会捕获异常并记录到日志文件中,确保错误信息的完整保存。
摘要由CSDN通过智能技术生成

1、首先配置web.xml  添加一下代码

500

/error.jsp

2、error.jsp页面

response.setStatus(HttpServletResponse.SC_OK);

%>

错误页面

function showErrorMessage(){

$("#errorMessageDiv").toggle();

}

$(document).ready(showErrorMessage);

                    error_title_icon.gif  错误提示                               

                    sorry.gif

尊敬的用户:
系统出现了异常,请重试。


如果问题重复出现,请向系统管理员反馈。

详细错误信息

 
  

try {

//全部内容先写到内存,然后分别从两个输出流再输出到页面和文件

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

PrintStream printStream = new PrintStream(byteArrayOutputStream);

printStream.println();

printStream.println("用户信息");

printStream.println("账号:" + request.getSession().getAttribute("userName"));

printStream.println("访问的路径: " + request.getAttribute("javax.servlet.forward.request_uri"));

printStream.println();

printStream.println("异常信息");

printStream.println(exception.getClass() + " : " + exception.getMessage());

printStream.println();

Enumeration 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);    //输出到网页

File dir = new File(request.getRealPath("/errorLog"));

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();

}

%>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值