通常,在系统遇到错误时,我们都会重定向到error.jsp来集中显示信息。一种较好的做法是显示给用户友善,好理解的信息,然后把exception trace通过log4j输出到后台log文件。
在显示信息给用户的时候,通常在页面上给一个唯一的ID,(这个ID可以是timestamp),然后在往后台log文件中写exception trace之前,输出这个ID。 所以,用户可以发给developer这个唯一的ID,而developer可以根据这个ID到log文件中查看具体错误信息。 这是 SIT、UAT常用的方法。
下面是代码片段:
=================================================================
<%
/*
*
*
* @File: $RCSfile: error.jsp,v $
* @version: $Revision: 1.7 $
* @modified: $Date: 2007/03/05 09:11:19 $
*
*/
%>
<%@page contentType="text/html; charset=UTF-8" isErrorPage="true" %>
<%@page import="java.text.*"%>
<%@page import="java.util.*"%>
<%@page import="java.net.InetAddress"%>
<%@page import="java.net.UnknownHostException"%>
<%
Logger logger = Logger.getLogger("error.jsp");
InetAddress ipAddress = null;
char ipDigital = 'N';
//use current timestamp as error key both to UI and log file.
java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
Calendar cal = Calendar.getInstance();
String strDateTime = formatter.format(cal.getTime());
//get the server's ipAddress
try{
ipAddress = InetAddress.getLocalHost();
if(null != ipAddress){
ipDigital = ipAddress.getHostAddress().charAt(ipAddress.getHostAddress().length()-1);
}
}catch(UnknownHostException e){}
if(null != exception){
logger.error("[Exception in error.jsp: Id = [ " + strDateTime + " ] at server [ " + ipDigital + " ]]");
logger.error(exception.getMessage(),exception);
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Error</title>
<link rel="stylesheet" href="<%=request.getContextPath()%>/css/sample.css" type="text/css" media="all" />
</head>
<body>
<div class="header1">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td class="tabTopL">Server Error </td>
<td class="tabTopR"> </td>
<td> </td>
</tr>
</table>
</div>
<div class="header2">Encounter Error </div>
<table class="sectionBorder">
<tr>
<td class="sectionTitle">Use Guide </td>
</tr>
<tr>
<td class="txtData">
An unexpected server error has been encountered.
Please report the following information to help desk:<br>
Server error on [ <%=strDateTime%> ] at server [ <%=ipDigital%> ]."
</td>
</tr>
</table>
<div class="header2">Error Message </div>
<img src="<%=request.getContextPath()%>/images/alert.gif" title="Alert" alt="Alert" width="17" height="15" />
<span class="txtData">
<%
String errMsg = null;
if(exception != null){
if(exception instanceof javax.faces.FacesException){
exception = ((javax.faces.FacesException)exception).getCause();
if ( exception instanceof javax.faces.el.EvaluationException) {
Throwable t = ((javax.faces.el.EvaluationException) exception).getCause();
if (t instanceof Exception) {
exception = (Exception) t;
}
}
}
//Note: For displaying the error messages, the following is just a temporary solution.
//Handle customized exceptions first..
if(exception instanceof OneException){
errMsg = "Duplicate key is not allowed, or it disobeys the unique violation. ";
}else if(exception instanceof org.springframework.jdbc.BadSqlGrammarException){
errMsg = "It would be possible to have subclasses for no such table, no such column etc. ";
}else if(exception instanceof org.springframework.jdbc.InvalidResultSetAccessException){
errMsg = "This typically happens when an invalid ResultSet column index or name has been specified. Also thrown by disconnected SqlRowSets. ";
}else if(exception instanceof org.springframework.dao.DataAccessResourceFailureException){
errMsg = "A resource fails completely, eg., if we can't connect to a database using JDBC. ";
}else if(exception instanceof org.springframework.dao.DataIntegrityViolationException){
errMsg = "Maybe when an attempt to insert or update data results in violation of an integrity constraint. ";
}else if(exception instanceof org.springframework.dao.CannotAcquireLockException){
//errMsg = "";
}...else{
errMsg = exception.getMessage();
}
}else{
errMsg = "System is busy, please try later.";
}
out.println(errMsg);
%>
</span>
<div class="gap5pix"></div>
<% if(exception != null){%>
<table class="sectionBorder">
<tr>
<td class="sectionTitle">Error Message Detail</td>
</tr>
<tr>
<td class="txtData">
<%
try {
out.write(exception.getMessage());
} catch ( Exception ex ) {
out.write("Unknown Message");
}
%>
</td>
</tr>
</table>
<%}%>
<br>
</body>
</html>
============================================================
error.jsp的截屏:
在显示信息给用户的时候,通常在页面上给一个唯一的ID,(这个ID可以是timestamp),然后在往后台log文件中写exception trace之前,输出这个ID。 所以,用户可以发给developer这个唯一的ID,而developer可以根据这个ID到log文件中查看具体错误信息。 这是 SIT、UAT常用的方法。
下面是代码片段:
=================================================================
<%
/*
*
*
* @File: $RCSfile: error.jsp,v $
* @version: $Revision: 1.7 $
* @modified: $Date: 2007/03/05 09:11:19 $
*
*/
%>
<%@page contentType="text/html; charset=UTF-8" isErrorPage="true" %>
<%@page import="java.text.*"%>
<%@page import="java.util.*"%>
<%@page import="java.net.InetAddress"%>
<%@page import="java.net.UnknownHostException"%>
<%
Logger logger = Logger.getLogger("error.jsp");
InetAddress ipAddress = null;
char ipDigital = 'N';
//use current timestamp as error key both to UI and log file.
java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
Calendar cal = Calendar.getInstance();
String strDateTime = formatter.format(cal.getTime());
//get the server's ipAddress
try{
ipAddress = InetAddress.getLocalHost();
if(null != ipAddress){
ipDigital = ipAddress.getHostAddress().charAt(ipAddress.getHostAddress().length()-1);
}
}catch(UnknownHostException e){}
if(null != exception){
logger.error("[Exception in error.jsp: Id = [ " + strDateTime + " ] at server [ " + ipDigital + " ]]");
logger.error(exception.getMessage(),exception);
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Error</title>
<link rel="stylesheet" href="<%=request.getContextPath()%>/css/sample.css" type="text/css" media="all" />
</head>
<body>
<div class="header1">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td class="tabTopL">Server Error </td>
<td class="tabTopR"> </td>
<td> </td>
</tr>
</table>
</div>
<div class="header2">Encounter Error </div>
<table class="sectionBorder">
<tr>
<td class="sectionTitle">Use Guide </td>
</tr>
<tr>
<td class="txtData">
An unexpected server error has been encountered.
Please report the following information to help desk:<br>
Server error on [ <%=strDateTime%> ] at server [ <%=ipDigital%> ]."
</td>
</tr>
</table>
<div class="header2">Error Message </div>
<img src="<%=request.getContextPath()%>/images/alert.gif" title="Alert" alt="Alert" width="17" height="15" />
<span class="txtData">
<%
String errMsg = null;
if(exception != null){
if(exception instanceof javax.faces.FacesException){
exception = ((javax.faces.FacesException)exception).getCause();
if ( exception instanceof javax.faces.el.EvaluationException) {
Throwable t = ((javax.faces.el.EvaluationException) exception).getCause();
if (t instanceof Exception) {
exception = (Exception) t;
}
}
}
//Note: For displaying the error messages, the following is just a temporary solution.
//Handle customized exceptions first..
if(exception instanceof OneException){
errMsg = "Duplicate key is not allowed, or it disobeys the unique violation. ";
}else if(exception instanceof org.springframework.jdbc.BadSqlGrammarException){
errMsg = "It would be possible to have subclasses for no such table, no such column etc. ";
}else if(exception instanceof org.springframework.jdbc.InvalidResultSetAccessException){
errMsg = "This typically happens when an invalid ResultSet column index or name has been specified. Also thrown by disconnected SqlRowSets. ";
}else if(exception instanceof org.springframework.dao.DataAccessResourceFailureException){
errMsg = "A resource fails completely, eg., if we can't connect to a database using JDBC. ";
}else if(exception instanceof org.springframework.dao.DataIntegrityViolationException){
errMsg = "Maybe when an attempt to insert or update data results in violation of an integrity constraint. ";
}else if(exception instanceof org.springframework.dao.CannotAcquireLockException){
//errMsg = "";
}...else{
errMsg = exception.getMessage();
}
}else{
errMsg = "System is busy, please try later.";
}
out.println(errMsg);
%>
</span>
<div class="gap5pix"></div>
<% if(exception != null){%>
<table class="sectionBorder">
<tr>
<td class="sectionTitle">Error Message Detail</td>
</tr>
<tr>
<td class="txtData">
<%
try {
out.write(exception.getMessage());
} catch ( Exception ex ) {
out.write("Unknown Message");
}
%>
</td>
</tr>
</table>
<%}%>
<br>
</body>
</html>
============================================================
error.jsp的截屏: