• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

Printing to a file from a jsp on a server.

I want to print stats to a file from a jsp, on the machine that the jsp is running on i.e.

the name of the machine is "test"  the jsp is running on a server on this machine
and i want to create a file on that server under a specific directory i.e


/test/logs/

where test is a root directory.

I also want to be able to append data to this file without it being over written, how
is it possible to do that.

Any help greatly appreciated.
0
pronane
Asked:
pronane
  • 5
1 Solution
 
TimYatesCommented:
<%
    java.io.BufferedWriter bw = null ;
    try
    {
        bw = new BufferedWriter( new FileWriter( "/test/logs/log.txt", true ) ) ;
        bw.write( "Hi there at " + new Date().toString() + "\n" ) ;
    }
    catch( java.io.IOException ex )
    {
        ex.printStackTrace() ;
    }
    finally
    {
        try { if( bw != null ) bw.close() ; } catch( java.io.IOException ex ) {}
    }
%>
0
 
TimYatesCommented:
Also, look at the apache.commons.logging libraries (which I think come with tomcat)

http://jakarta.apache.org/commons/logging/guide.html
0
 
pronaneAuthor Commented:
cheers tim

does the write method append to a file OR WRITE over previous content?

Also what if any given folder on that server isnt world writeable, will the jsp
still be able to create a file under that folder?
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
TimYatesCommented:
> new FileWriter( "/test/logs/log.txt", true )

The "true" parameter means it will append to the file :-)
0
 
TimYatesCommented:
> Also what if any given folder on that server isnt world writeable, will the jsp
> still be able to create a file under that folder?

It depends on the access rights of the tomcat process...

If the user that is considered to be running tomcat has no write access to that folder, it will get an IOException, and not create/append that file...
0
 
TimYatesCommented:
*sniff* only a B grade :-(

Good luck with it...

Tim
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now