Solved

Temporary Files and Servlets or JSP

Posted on 2004-10-21
281 Views
Last Modified: 2010-04-01
Hi,

I am writing a class that will be used from within a JSP or Servlet.

If an exception occurs then I want to be able to create a temporary file and output detailed info to there. I want to create a temporary html file for this. The reason for this is there is a lot of "rich" content that cannot be described as text in an exception message.

In the exception message I would tell the user to look at this file.

I would like to know:

1) Can temporary files be reliably created somehow? Or will there be permissions problems sometimes?

2) will these files be deleted themselves eventually or am I responsible for that?

thanks!
0
Question by:ycomp
    11 Comments
     
    LVL 8

    Expert Comment

    by:kiranhk
    1) you can create temporary files on the Server. there will not be any security problems while creating the files on the server side
    2) no, you need to take care of deleting the files

    this thread might be of some help

    http://archives.java.sun.com/cgi-bin/wa?A2=ind9901&L=servlet-interest&F=&S=&P=105914
    0
     

    Author Comment

    by:ycomp
    thanks for the quick answer!

    I don't really want to add the overhead of deleting files so now I'm thinking just to use the same name every time.

    e.g. "debugdump.html" and have it created in the default temporary directory.

    How do I do that? just new File("debugdump.html") ? would that put it in the temp dir or in the current dir? What about new FileWriter("debugdump.html")?
    0
     
    LVL 13

    Expert Comment

    by:petmagdy

    U need to lock into Log4J get it from jakarta.apache.com
    0
     
    LVL 8

    Expert Comment

    by:kiranhk
    i have not used that,
    u can also check out the JDK logging api or as petmagdy suggested the Log4J

    http://javaalmanac.com/egs/java.util.logging/pkg.html
    0
     

    Author Comment

    by:ycomp
    I see... but I need to create a richly formatted HTML document. it is not logging really. It is an illustration of what went wrong that led to this particular exception.
    0
     
    LVL 8

    Expert Comment

    by:kiranhk
    then better go in for file writing, u can get lots of example code for the same from the above link which i gave u
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    take a more look log4j supports multiple formats
    0
     

    Author Comment

    by:ycomp
    ok, thanks.

    does anyone have an answer to this though?

    -------
    I don't really want to add the overhead of deleting files so now I'm thinking just to use the same name every time.

    e.g. "debugdump.html" and have it created in the default temporary directory.

    How do I do that? just new File("debugdump.html") ? would that put it in the temp dir or in the current dir? What about new FileWriter("debugdump.html")?
    ---------
    0
     
    LVL 2

    Accepted Solution

    by:
    something like this:-
    static String path="D:\\log";
          public void  fileDumpEx(Exception e) {
                try {
                      FileWriter f=new FileWriter(path+File.separator+"debugdump.html");
                      f.write("<html><body>");
                      f.write(e.getClass().getName()+" - " +e.getMessage()+"<br/>");
                      StackTraceElement[] stack=e.getStackTrace();
                      for (int i=0;i<stack.length;i++) {
                            f.write(stack[i].getLineNumber());
                            f.write(":");
                            f.write(stack[i].getClassName());
                            f.write("#");
                            f.write(stack[i].getMethodName());
                            f.write("<br/>\n");
                      }
    //                  this just dumps in the stacktrace as text.
    //                  f.write("output of stacktrace.");
    //                  PrintWriter pw=new PrintWriter(f);
    //                  e.printStackTrace(pw);
    //                  
                      f.write("</body></html>");
                      f.close();
                } catch (IOException e1) {
                      
                }
                finally {
                      
                }
                
                
          }
          public void method() {
                String h=null;
                try {
                      h.toString();
                }
                catch (Exception e) {
                      fileDumpEx( e);
                      
                }
          }
    0
     
    LVL 2

    Expert Comment

    by:siliconeagle
    did this help? havent heard any more on this thread.
    0
     

    Author Comment

    by:ycomp
    sorry I got caught up on other things. I'll get back to this eventually and accept an answer. thanks for your help.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    Suggested Solutions

    Title # Comments Views Activity
    Problem with Pie chart in Jsp 20 140
    bean mapper frameworks 5 53
    console vs log file 16 71
    I get error: useBean: Duplicate bean name: {0} 1 35
    New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
    Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now