Configuring a Tomcat webapp to serve static content from external directory

旧城冷巷雨未停 提交于 2019-12-11 14:03:44

问题


I hope this is not too trivial - I'm very new at Tomcat administration.

I'm building the development deployment process for a brand-new web application. We have the client assets and the web services the client will talk to in separate directories. The services are served from a Tomcat 6 webapp, being served at /my-app/services/.

I need to update the configuration such that the Tomcat webapp will serve up the static client assets from the client directory as well as the web services. Ideally, these static assets will be served from /my-app/, with just the services directory reserved for the dynamic content.

Currently tomcat is configured with the single webapp containing a trivial context.xml in META-INF. I have tried setting up two contexts manually in server.xml, and also setting both contexts up in tomcat/conf// (naming them my-app#services.xml and my-app.xml respectively, and setting the docBase of my-app.xml to the relative path to the client assets directory). Setting it up like this results in the web services content no longer successfully starting (failing to find the required classes), but the static content is now served from the correct path.

I'm really at a loss as to what to do here. If somebody with Tomcat experience could just sketch out the appropriate solution for this problem, I would be so grateful. If the functionality I've described above is not workable, minor changes are totally feasible - I just want to get something working so that I can go back to writing code!

NB: We want to keep the static client assets separate from the services, as this lets us keep the two systems decoupled, and also lets us more easily do fancy things with client deployment such as watching the source files and re-running the unit tests when things change.

Thanks in advance for any help offered.


回答1:


You should add in server.xml
<Context path="/my-app/static" docBase="/someFolder/my-app/static-web" debug="0" reloadable="true" crossContext="false"/>

Experiment with slashes. It wouldn't work if slashes is incorrect.
path - is URL where your static files will be accessible.
docBase - folder at hard disk with static files
I guess you cannot map many contextes on single path.

Another trick is to write a servlet/filter which will return static context from folder



来源:https://stackoverflow.com/questions/16908653/configuring-a-tomcat-webapp-to-serve-static-content-from-external-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!