【问题标题】:Alternatives for PrintStacktrace() method in jspjsp 中 PrintStacktrace() 方法的替代方案
【发布时间】:2014-02-18 15:54:48
【问题描述】:

我最近通过 PMD 运行了我的代码。还有这个jsp,有人在其中写了一个try catch块(我知道这是一种不好的做法。)。

代码是这样的

<%
 Thread t = new Thread(runA);
 String tClassName = null;
 tClassName = request.getParameter("classname");
 t.setName(tClassName);
  t.start();
 %>


 <%!
   Runnable runA = new Runnable(){
    public void run(){
      try{
          // some code here   
          }
      catch(Exception e){
         e.printStackTrace();            
          }

          }
         };
    %>

现在 PMD 工具建议不要使用printStackTrace() 方法,因为它可能会导致安全漏洞。

有人可以建议printStackTrace() 的替代方法,在catch 块内使用。请注意此代码位于 jsp 页面上。

【问题讨论】:

  • JSP 中的 Java 代码?不推荐。
  • 不是 try-catch 不是一个好主意,而是根本就存在 scriptlet。

标签: java jsp try-catch printstacktrace


【解决方案1】:

如果要记录异常,使用一些日志框架(如Log4j)并使用

log.error("Ops!", e);

好处是日志框架记录到服务器上正确的日志文件。 e.printStackTrace() 只写入System.err

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多