Logging 如何在J2EE应用程序中全局捕获错误、记录错误并向用户显示错误页面
我在谷歌上搜索了一下这个话题,看到了一些最佳实践。但我需要一些具体的建议。我正在开发一个J2EE应用程序,它具有servlet/Struts2/从JSP调用DAO。因此,应用程序是各种各样的混乱。大多数数据是通过存储过程获取的,这些存储过程由iBatis ORM/Spring调用。有时,当SP端发生错误时,它会向用户显示一条丑陋的消息,如下所示:Logging 如何在J2EE应用程序中全局捕获错误、记录错误并向用户显示错误页面,logging,jakarta-ee,error-handling,ora-06550,Logging,Jakarta Ee,Error Handling,Ora 06550,我在谷歌上搜索了一下这个话题,看到了一些最佳实践。但我需要一些具体的建议。我正在开发一个J2EE应用程序,它具有servlet/Struts2/从JSP调用DAO。因此,应用程序是各种各样的混乱。大多数数据是通过存储过程获取的,这些存储过程由iBatis ORM/Spring调用。有时,当SP端发生错误时,它会向用户显示一条丑陋的消息,如下所示: javax.servlet.ServletException: org.springframework.jdbc.BadSqlGrammarExcep
javax.servlet.ServletException: org.springframework.jdbc.BadSqlGrammarException: SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in debtowed.xml.
--- The error occurred while applying a parameter map.
--- Check the debtowed.getFormerTenantData.
--- Check the statement (update procedure failed).
--- Cause: java.sql.SQLException: ORA-06550: line 1, column 11:
PLS-00905: object package.GET_FORMER_ADDRESS is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Caused by: java.sql.SQLException: ORA-06550: line 1, column 11:
PLS-00905: object package.GET_FORMER_ADDRESS is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
此时,上述信息将显示在浏览器中并记录到server.log。但是,我想做以下几点:
- 向用户显示自定义错误页面
- 将错误记录在myapp.log而不是server.log中(我们在其他一些地方使用log4j时会这样做)
getSqlMapClientTemplate().queryForList("sp.getfmrAddy", paramMap);
我希望Struts已经以某种方式解决了这个问题,所以最好检查Struts docco。如果我是你,我会编写一个ExceptionFilter来包装你的servlet调用:
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class ExceptionFilter implements Filter{
private static final Logger logger = Logger.getLogger(ExceptionFilter.class);
private ExceptionService exceptionService = null;
@Override
public void destroy() {
exceptionService.shutdown();
}
@Override
public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain chain) throws IOException, ServletException {
try {
chain.doFilter(rq, rs); // this calls the servlet which is where your exceptions will bubble up from
} catch (Throwable t) {
// deal with exception, then do redirect to custom jsp page
logger.warn(t);
exceptionService.dealWithException(t); // you could have a service to track counts of exceptions / log them to DB etc
HttpServletResponse response = (HttpServletResponse) resp;
response.sendRedirect("somejsp.jsp");
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
exceptionService = new ExceptionService();
}
}
并将其添加到web.xml中:
<filter>
<filter-name>ExceptionFilter</filter-name>
<filter-class>com.example.ExceptionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ExceptionFilter</filter-name>
<servlet-name>MyMainServlet</servlet-name>
</filter-mapping>
例外过滤器
com.example.ExceptionFilter
例外过滤器
MyMainServlet
然后为所有servlet添加过滤器映射
希望对您有所帮助。使用java util日志记录包它的易用性可以通过后端的每个属性方法声明,而在前端,您希望让错误对用户可见,可以使用xhtml或jsf中的h:message标记或f:ajax错误处理标记来显示它