2

how to print exception stack trace to web page?

import com.opensymphony.xwork2.ActionSupport;
import java.util.Arrays;

public class ExceptionAction extends ActionSupport 
{

 public String execute() 
 {      
    //
    String errMsg = Arrays.toString(Thread.currentThread().getStackTrace());

    this.addActionError(
                    "Unexcepted error has occurred. \n"
                    +errMsg
                    + "\nPlease try again or report this error to    administrator or support team. \n ");

    return "error";
 }

 /**
 * 
 */
 private static final long serialVersionUID = -3754090784536811052L;
}

but i got a useless msg use above codes, msg is like this:

    [java.lang.Thread.getStackTrace(Thread.java:1479),    com.monitoring.action.ExceptionAction.execute(ExceptionAction.java:11), sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method), sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39), 
Scott 混合理论
  • 2,263
  • 8
  • 34
  • 59
  • 1
    to Pages you mean , to iWork Pages ? or onto webpage ? – Raptor Jun 22 '13 at 07:19
  • Try to throw exception where it is occurred, all trace should be on web page, but I do not think that can help you in your problem. – Sergii Zagriichuk Jun 22 '13 at 07:23
  • In general, you *don't* want to print stack traces on web pages. It can reveal a lot and *help* people gain access to your servers without your consent :) – devnull Jun 22 '13 at 08:21
  • Many options here: http://stackoverflow.com/questions/1149703/stacktrace-to-string-in-java including the utility methods from Guava and commons-lang – samlewis Jun 22 '13 at 10:34

2 Answers2

3

Here is code to print stack trace to a page

 package test;    
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        PrintWriter out = response.getWriter();
        try {
            "test".charAt(20);
        } catch (RuntimeException e) {
            out.println("Stack Trace:<br/>");
            e.printStackTrace(out);
            out.println("<br/><br/>Stack Trace (for web display):</br>");
            out.println(displayErrorForWeb(e));
        }
    }

    public String displayErrorForWeb(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        t.printStackTrace(pw);
        String stackTrace = sw.toString();
        return stackTrace.replace(System.getProperty("line.separator"), "<br/>\n");
    }

}

Source :http://www.avajava.com/tutorials/lessons/how-do-i-display-a-stack-trace-on-a-web-page.html

Sain Pradeep
  • 3,119
  • 1
  • 22
  • 31
2

See documentation

File file = new File("test.log");
PrintStream ps = new PrintStream(file);
try {
    // something
} catch (Exception ex) {
    ex.printStackTrace(ps);
}
ps.close();

See also Difference between printStackTrace() and toString()

Community
  • 1
  • 1
Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57