How do I printStackTrace to PrintStream

I'm writing a JSP file that query the DB and process the results, i have put my code within try-catch and i want to catch(Throwable t). My question is how can I print the stackTrace to a PrintStream then to a text file.

Thank you for your assistance.

Ayad
AlqershiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrcoffee365Commented:
This is one example:
http://www.rgagnon.com/javadetails/java-0029.html

Basically, you create a PrintWriter and pass it to e.printStackTrace .
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rrzCommented:
Please try this JSP. Look in your web app's root folder for the file errorStream.txt  .
<%@ page import="java.io.*"%>
<%
  File f = new File(application.getRealPath("/errorStream.txt"));
  try{
      String s = null;
      s.trim();
  }catch(Exception e){
                      PrintStream ps = new PrintStream(f);
                      System.setErr(ps);
                      e.printStackTrace();
                      ps.close();
   }
%>
hello

Open in new window

0
rrzCommented:
On second thought, mrcoffee's idea is better.  Try the following.
<%@ page import="java.io.*"%>
<%
  try{
 
      String s = null;
      s.trim();
 
     }catch(Exception e){
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        e.printStackTrace(pw);
                        File f = new File(application.getRealPath("/errorStream.txt"));
                        FileWriter fw = new FileWriter(f,true);
                        fw.write(sw.toString());
                        fw.close();
      }
%>
hello2

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

rrzCommented:
maybe add
pw.close();
0
rrzCommented:
You should have split the points. It was mrcoffee's idea.
0
rrzCommented:
I made a request for attention.
0
mrcoffee365Commented:
Thanks rrz@871311.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.