Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 382
Chris Dutrow Avatar asked Mar 12 '10 18:03

Chris Dutrow


2 Answers

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.

like image 62
Konrad Garus Avatar answered Nov 03 '22 11:11

Konrad Garus


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.

like image 33
Teja Kantamneni Avatar answered Nov 03 '22 09:11

Teja Kantamneni