Search code examples
javatomcatfile-uploadservletcontextlistener

Change Temporary Directory for Apache File Upload in Tomcat


I am having a difficult time trying to change the default location of the "javax.servlet.context.tempdir" in our Tomcat's ServletContext and it just isn't working.

Here is the long and short of it. When inspecting our ServletContext in a debugger, the attribute "javax.servlet.context.tempdir" is a read-only attribute and doesn't change when the setAttribute method is called on the ServletContext.

How can we change this attribute?


Solution

  • The javax.servlet.context.tempdir ServletContext attribute can be set one of two ways.

    In your server.xml as an attribute of the Host element.

    <Host name="localhost" workDir="directory_location">
        ...
    </Host>
    

    Tomcat 8 reference on Host

    Or in your context.xml as an attribute of the Context element.

    <?xml version='1.0' encoding='utf-8'?>
    <Context workDir="directory_location">
        ...
    </Context>
    

    Tomcat 8 reference on Context

    WARNING

    Changing this value also has the side effect of changing where tomcat stores the JSP files it compiles into classes.