Printing to a file from a jsp on a server.

Posted on 2004-10-25
Last Modified: 2010-04-01
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


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.
Question by:pronane
    LVL 35

    Accepted Solution

    <% bw = null ;
            bw = new BufferedWriter( new FileWriter( "/test/logs/log.txt", true ) ) ;
            bw.write( "Hi there at " + new Date().toString() + "\n" ) ;
        catch( ex )
            ex.printStackTrace() ;
            try { if( bw != null ) bw.close() ; } catch( ex ) {}
    LVL 35

    Expert Comment

    Also, look at the apache.commons.logging libraries (which I think come with tomcat)
    LVL 1

    Author Comment

    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?
    LVL 35

    Expert Comment

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

    The "true" parameter means it will append to the file :-)
    LVL 35

    Expert Comment

    > 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...
    LVL 35

    Expert Comment

    *sniff* only a B grade :-(

    Good luck with it...


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    Title # Comments Views Activity
    maven scope 1 113
    Coldfusion 10 - JSP File Not Found - 404 Error 5 47
    squirrelPlay java challenge 40 154
    java operators 3 54
    Phishing is at the top of most security top 10 efforts you should be pursuing in 2016 and beyond. If you don't have phishing incorporated into your Security Awareness Program yet, now is the time. Phishers, and the scams they use, are only going to …
    Know what services you can and cannot, should and should not combine on your server.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    931 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

    14 Experts available now in Live!

    Get 1:1 Help Now