Alternatives for PrintStacktrace() method in jsp

1.2k views Asked by At

I have recently run my code through PMD. And there's this jsp, in which someone has written, a try catch block (I know its a bad practice.) .

The code goes something like this

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

          }
         };
    %>

Now the PMD tool suggests not using the printStackTrace() method, since it is likely to cause security vulnerability.

Could Somebody please suggest an alternative for the printStackTrace(), to use inside the catch block. And please note this code is on a jsp page.

1

There are 1 answers

0
Petr On BEST ANSWER

If you want to log the exception, it is more appropriate to use some logging framework (like Log4j) and use

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

The benefit is the logging framework logs to a correct log file on server. The e.printStackTrace() writes just to the System.err.