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 .

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

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

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

rrzCommented:
maybe add
pw.close();
rrzCommented:
You should have split the points. It was mrcoffee's idea.
rrzCommented:
I made a request for attention.
mrcoffee365Commented:
Thanks rrz@871311.
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.