Chong We Tan
Chong We Tan

Reputation: 243

Tomcat context docbase

I'm using tomcat 8, I have a function that retrives and updates the profile picture. The files are in an external folder. Retrieved using this code in servlet.xml

<Context docBase="C:/assets" path="mywebapp/files"/>

It's working fine in my local tomcat but when accessing it in a remote server the newly created files are not being displayed. I have to restart tomcat in the server so that the new images would get displayed.

I also tried this

<Context docBase="C:/assets" path="mywebapp/files" reloadable="true"/>

but still it didn't work

Any ideas how to not have to restart tomcat?

Upvotes: 0

Views: 2006

Answers (1)

J. Michaud
J. Michaud

Reputation: 11

I believe your docBase line belongs in server.xml, not servlet.xml. I also think your path variable needs to start with a leading slash. I don't know if it can contain two levels, you might want to just change it to path=/assets

Next, look at your context.xml file. If it says

<Context antiResourceLocking="true">

you need to reload the context before the new file will be available. If your Context element does not have antiResourceLocking="true", then the file should be immediately available.

You can reload the context programmatically, without restarting Tomcat, by issuing a GET request to http://localhost:8080/manager/text/reload?path=/assets (assuming you change your path variable to /assets)

However you'll probably need to provide an Authenticator, like this:

Authenticator.setDefault (new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication ("tomcat", "password".toCharArray());
        }
    });

    URL url = new URL("http://localhost:8080/manager/text/reload?path=/assets");

    try {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.getResponseCode();

        BufferedReader in = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

        logger.info(response.toString());
        in.close();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }

Upvotes: 1

Related Questions