Tomcat Replacing VM arguments

六眼飞鱼酱① 提交于 2019-12-08 07:23:02

问题


this may be a stupid question:

I currently have some VM arguments that my web application requires. They are something like:

-Dcom.company.custom.var='test'

And I access them in code with:

System.getProperty("com.company.custom.config")

I would like to move them into one of the tomcat xml files (server.xml, context.xml, etc) but I'm not sure how I would go about doing that.

So my question is two parts. Where (and how) should I put these variables into xml and how do I access them in code?

Thanks in advance and sorry if this has already been answered. My search-fu on this topic has failed me.


回答1:


Officially, all JVM options go either to setenv.sh (for Unix) or setenv.bat (for Windows). This file is not present by default in Tomcat but if you create one, it will be picked up by startup script.

The reason why you should be using this instead of server.xml is that server.xml might change from one Tomcat version to another but setenv.sh (or .bat) is completely custom to you.

Reading this value is, as you pointed out, through System.getProperty(...).




回答2:


If these are app-specific configuration items, the standard location for it is a <context-param> in web.xml. Something like this:

<context-param>
    <param-name>myvar</param-name>
    <param-value>value</param-value>
</context-param>

You can access these values from code using ServletContext#getInitParameter(String). From inside a servlet, you could write something like:

String myvar = getServletContext().getInitParameter("myvar");


来源:https://stackoverflow.com/questions/12372550/tomcat-replacing-vm-arguments

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