3

I have a web application deployed to Tomcat 6 as a WAR file. From within the application I would like to serve files in a directory from within that context. For example, I deploy myapp.war to /mydir/webapps and I access my application via:

http://myhost:myport/myapp/

I would like to serve files via:

http://myhost:myport/myapp/files/somefile.txt

How can I do this? So far, I am only able to serve files outside of the application context:

http://myhost:myport/files/somefile.txt

with the files sitting in /mydir/webapps/files instead of in the /mydir/webapps/myapp/files directory.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Brian
  • 155
  • 1
  • 2
  • 9

2 Answers2

1

Use a sub-context that points to the external directory.

Based on the paths in your example, you want to add the following file: $CATALINA_BASE/<enginename>/<hostname>/myapp#files.xml (normally $CATALINA_BASE/Catalina/localhost/myapp#files.xml

with the following contents

<Context docBase="/mydir/webapps/files" />

Note that for this to work safely, /mydir/webapps/files must be outside any Host's appBase where autoDeploy is enabled.

Mark Thomas
  • 16,339
  • 1
  • 39
  • 60
-1

Do you have set <display-name>myapp</display-name> in your /mydir/webapps/myapp/META-INF/web.xml ?

dr0i
  • 19
  • 3
  • The web.xml file is inside the WAR rather than on the file system, and inside it I find `Web_myapp`. The `/mydir/myapp` directory did not exist until I created it. This was after deploying `myapp.war`. – Brian Apr 04 '12 at 13:29
  • You may need to deploy your webapp as so the WAR "explodes" (expands) into the filesystem (I ever only use this type of deploying ) because you need the filesystem to serve files. Look at https://tomcat.apache.org/tomcat-6.0-doc/deployer-howto.html how to do that. You have to set "deployOnStartup" attribute to "true". – dr0i Apr 04 '12 at 13:42
  • This is a question rather than an answer. Also the display-name tag has zero relevance to the question. It only affects how the application is displayed in any management tools that may be used. It has no impact on context path. – Mark Thomas Apr 04 '12 at 18:35