Vinodh Ravi
Vinodh Ravi

Reputation: 67

Host a json file that is outside the war file in tomcat

I have a json file that is in webapps folder of tomcat but outside my project folder. I need this file to be seen on browser through http:localhost:8080/"somePath".

I tried adding the line : Context docBase="C:/Users/abc/tomcat/webapps" path="/static" /> to server.xml and tried to hit the URL : http:localhost:8080/static/file.json but I am getting 404 not found.

Upvotes: 1

Views: 3174

Answers (1)

Richard Osseweyer
Richard Osseweyer

Reputation: 1742

Better not define Context nodes in server.xml: https://tomcat.apache.org/tomcat-8.0-doc/config/context.html#Defining_a_context

Instead create a file

$CATALINA_BASE/conf/[enginename]/[hostname]/static.xml

So in your case I suppose that is:

C:/Users/abc/tomcat/conf/Catalina/localhost/static.xml

And put a Context node in it:

<Context path="/static/"
         docBase="C:/Users/abc/tomcat/webapps/static_files/">
</Context>

Here the json file is in a subdirectory static_files. I wouldn't expose the entire webapps directory to the static path. docBase need not even be in webapps, it could point to a directory anywhere on your filesystem.

Reboot and your json file will be available at

http:localhost:8080/static/file.json

Upvotes: 1

Related Questions