How is user.dir configured for Tomcat servlets?

巧了我就是萌 提交于 2020-01-04 05:56:14

问题


In my Tomcat servlet, I am using a jar library that looks for certain .properties files in System.getProperty("user.dir").

The issue is that when I deploy the servlet on different servers, the "user.dir" is different. I'd like them to be the same. Is there a way to configure this directory so that it is the same for each server?

I also ran catalina.sh version, and the "user.dir" directory doesn't appear in the output (including CATALINA_BASE and CATALINA_HOME), which is strange. This happens for some of the servers. On other servers, the "user.dir" matches CATALINA_BASE.


回答1:


Y̶o̶u̶ ̶c̶o̶u̶l̶d̶ ̶s̶e̶t̶ ̶t̶h̶e̶ ̶p̶r̶o̶p̶e̶r̶t̶y̶ ̶m̶a̶n̶u̶a̶l̶l̶y̶ ̶b̶y̶ ̶m̶o̶d̶i̶f̶y̶i̶n̶g̶ ̶t̶h̶e̶ ̶̶c̶a̶t̶a̶l̶i̶n̶a̶.̶s̶h̶̶ ̶a̶n̶d̶ ̶s̶e̶t̶t̶i̶n̶g̶ ̶i̶t̶ ̶t̶h̶e̶r̶e̶.̶ ̶B̶u̶t̶ ̶t̶h̶i̶s̶ ̶a̶p̶p̶r̶o̶a̶c̶h̶ ̶w̶o̶u̶l̶d̶ ̶b̶e̶ ̶v̶e̶r̶y̶ ̶c̶o̶m̶b̶e̶r̶s̶o̶m̶e̶ ̶s̶i̶n̶c̶e̶ ̶y̶o̶u̶ ̶h̶a̶v̶e̶ ̶t̶o̶ ̶s̶e̶t̶ ̶t̶h̶e̶ ̶v̶a̶l̶u̶e̶ ̶o̶f̶ ̶t̶h̶i̶s̶ ̶f̶o̶l̶d̶e̶r̶ ̶m̶a̶n̶u̶a̶l̶l̶y̶ ̶i̶n̶ ̶e̶v̶e̶r̶y̶ ̶t̶o̶m̶c̶a̶t̶ ̶i̶n̶s̶t̶a̶l̶l̶a̶t̶i̶o̶n̶.̶

Paragraph up here is mistaken. This is because I naively forgot that "user.dir" property is the working folder where Java was executed (example shown here) and you cannot change it not even with -D property when executing the JVM. If you want to change the value of "user.dir", you would have to change the folder where Java executes to start tomcat, and this can be easy or hard, depending on your skills, and I do not recommend this approach as a solution.

A̶n̶o̶t̶h̶e̶r̶ ̶o̶p̶t̶i̶o̶n̶ The best option would be to define a ServletContextListener and set a new property there (and the value as a specific folder), in the contextInitialized method. Then, in any of your servlets, you should recover this property instead.



来源:https://stackoverflow.com/questions/27822600/how-is-user-dir-configured-for-tomcat-servlets

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