English 中文(简体)
我如何从全球范围收集错误,将其记录下来,并显示用户在J2EE仪器中的错误页面
原标题:how do I catch errors globally, log them and show user an error page in J2EE app

我借着眼光探讨了这个议题,并看到了一些最佳做法。 但我需要一些具体的建议。 我正在准备一份J2EE仪器,该仪器从JP s处向DAO提供。 因此,这是各种东西。 大部分数据都是通过存储程序进行的,由iBatis ORM/Spring提出。 有时,在SP一侧发生错误时,它将向用户传递一个简单的信息,例如:

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

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

  • Present user with custom error page
  • log the errors in myapp.log instead of server.log (we do this in some other places as we use log4j)

les告诉我,这样做的最佳方式是什么? 我是否应在网上添加一些内容? 就像听众一样? 这将是唯一的变化,还是我必须修改现行法典?

该法典没有检查任何错误。 它只是说下面的SP。

getSqlMapClientTemplate().queryForList("sp.getfmrAddy", paramMap);
最佳回答

我期望斯特鲁特人已经以某种方式解决这一问题,因此,最好的检查是休战。 如果是你的话,你会写一个可以总结你的电灯的特例:

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

}

在您的网站上添加:

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

然后,增加所有服务器的过滤图。

希望会有所帮助。

问题回答

可在前端使用每一种财产方法宣布其易于使用。 在前端,如果你想要在用户使用h:message tag in xhtml or jsf, 或者还有 f:ajax处理标签错误





相关问题
Separating Business Layer Errors from API errors

The title is horrible, i know; I m terrible at titles on SO here. I m wondering what would be the best way to present unified error responses in a webapi when errors could be raised deep inside the ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

How to tell why a file deletion fails in Java?

File file = new File(path); if (!file.delete()) { throw new IOException( "Failed to delete the file because: " + getReasonForFileDeletionFailureInPlainEnglish(file)); } Is there a ...

Exceptions: redirect or render?

I m trying to standardize the way I handle exceptions in my web application (homemade framework) but I m not certain of the "correct" way to handle various situations. I m wondering if there is a ...

热门标签