Best practices for defining and initializing variables in web.xml and then accessing them from Java code

痴心易碎 提交于 2019-12-07 03:22:37

问题


I would like to define and initialize some variables in web.xml and the access the values of these variables inside my Java application.

The reason I want to do this is because I would like to be able to change the values of these variables without having to recompile the code.

What is the best practice for doing this? Most of the variables are just strings, maybe some numbers as well. Does the class that accesses the variables have to be a servlet?

Thanks!

Chris


回答1:


You could use your own resources, like Properties files, and place them somewhere in the classpath. Then you could read them into Properties and use from wherever you find convenient.

web.xml is best left for servlet context, not a general purpose resource.




回答2:


Why do you want to put those values in web.xml. Web.xml is primarily to configure the application not to set the bootstrap variables. The standard in the java world for such cases is using either a properties file or an xml in case you have some complex data structure.



来源:https://stackoverflow.com/questions/2434888/best-practices-for-defining-and-initializing-variables-in-web-xml-and-then-acces

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