How to change working directory in Tomcat

Posted on 2004-11-11
Last Modified: 2010-04-01
Hi all,

How to change working directory in Tomcat? Currently from my JSP I'm creating file and after that I'm finding this file in TOMCAT_HOME/bin, but I don't want to be there, I would like to be in my ROOT directory!
I know that I have to write something in web.xml or server.xml, but I don't know what exactly?!?!
Is there anybody that knows how to change my working directory?
Please write the lines that I have to add to this xml files in order to change my working directory!
Thanks in advance!
Question by:Valeri
    LVL 16

    Author Comment

    All that I need is to create this file in my Root directory. How to get the root directory and to create the file in this directory?
    LVL 13

    Accepted Solution

    try this:

    String rootdir = getServletConfig().getServletContext().getRealPath("/");

    use this directory to create the file
    LVL 35

    Assisted Solution

        String path = getServletContext().getRealPath( "." ) ;
        path = path.substring( 0, path.length() - 1 ) ;
        File f = new File( path + "" ) ;

    Should do it...
    LVL 35

    Expert Comment

    hehehe...  darn ;-)
    LVL 16

    Author Comment

    Only getServletContext().getRealPath( "." ) doesn't works...  "getServletConfig()" must be added in front. But what actually retruns this "getServletConfig()" ?!?!
    LVL 35

    Expert Comment

    All JSPs are compiled into servlets, and getServletConfig() is a method of HttpServlet :-)

    Hope it's all working!

    Good luck!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
    Great sound, comfort and fit, excellent build quality, versatility, compatibility. These are just some of the many reasons for choosing a headset from Sennheiser.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    758 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

    13 Experts available now in Live!

    Get 1:1 Help Now