我如何全局捕获错误,记录它们并向用户显示 J2EE 应用程序中的错误页面 [英] how do I catch errors globally, log them and show user an error page in J2EE app

查看:11
本文介绍了我如何全局捕获错误,记录它们并向用户显示 J2EE 应用程序中的错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌上搜索了这个话题,看到了一些最佳实践.但我需要一些具体的建议.我正在开发一个 J2EE 应用程序,它具有 servlets/Struts2/Call to DAO's from JSP's.所以这个应用程序是各种乱七八糟的.大多数数据是通过存储过程获取的,这些过程被 iBatis ORM/Spring 调用.有时,当 SP 端发生错误时,它会向用户显示丑陋的消息,如下所示:

I have searched this topic on google a bit and seen some best practices. But I need some specific advice. I am working on a J2EE app that has servlets/Struts2/Call to DAO's from JSP's. So the app is all kinds of messed up. Most of the data is fetched via stored procedures, which are being called by iBatis ORM/Spring. Sometimes when an error happens on the SP side it will display an ugly message to the user such as below:

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中.
但是,我想做以下事情:

At this point the above information is displayed in the browser and logged to server.log.
However, I want to do following:

  • 向用户展示自定义错误页面
  • 在 myapp.log 而不是 server.log 中记录错误(我们在其他一些地方这样做,因为我们使用 log4j)

请告诉我这样做的最佳方法是什么?我应该在 web.xml 中添加一些东西吗?像一个倾听者?这是唯一的更改还是我必须更改现有代码?

Plese tell me what is the best way of doing this? Should I be adding something in web.xml? like a listener? will that be the only change or will I have to change existing code?

代码不做任何错误检查.它只是像下面一样调用 SP

The code does not do any error checking. it just calls the SP like below

getSqlMapClientTemplate().queryForList("sp.getfmrAddy", paramMap);

推荐答案

我希望 Struts 已经以某种方式解决了这个问题 - 所以最好检查 struts docco.如果我是你,我会写一个 ExceptionFilter 来包装你的 servlet 调用:

I expect this is already solved in some way by Struts - so best check struts docco. If i were you i'd write an ExceptionFilter that can wrap your servlet calls:

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

and add this to your 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>

然后为所有 servlet 添加过滤器映射.

then add filter mappings for all your servlets.

希望有所帮助.

这篇关于我如何全局捕获错误,记录它们并向用户显示 J2EE 应用程序中的错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆