Solved

Printing to a file from a jsp on a server.

Posted on 2004-10-25
353 Views
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


/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
Question by:pronane
    6 Comments
     
    LVL 35

    Accepted Solution

    by:
    <%
        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
     
    LVL 35

    Expert Comment

    by:TimYates
    Also, look at the apache.commons.logging libraries (which I think come with tomcat)

    http://jakarta.apache.org/commons/logging/guide.html
    0
     
    LVL 1

    Author Comment

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

    Expert Comment

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

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

    Expert Comment

    by:TimYates
    > 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
     
    LVL 35

    Expert Comment

    by:TimYates
    *sniff* only a B grade :-(

    Good luck with it...

    Tim
    0

    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 (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. 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