How to get context-param value in java?
The “context-param” tag is define in “web.xml” file and it provides parameters to the entire web application.
For example, store administrator’s email address in “context-param” parameter to send errors notification from our web application.
web.xml
<context-param>
<param-name>AdministratorEmail</param-name>
<param-value>[email protected]</param-value>
</context-param>
We can get the above “AdministratorEmail” context-param value with the following java code.
String email= getServletContext().getInitParameter("AdministratorEmail");
Result
[email protected]
I read some more on getServletContext() and I see I can’t access it where I am. I am trying to initialize the webapp (read in config files, setup logging, etc.) via the contextInitializated method while the examples say doGet in a extended httpServlet to get access. So I will have to find some other way to pass in a variable for the logging path.
I tried this in my serlvet and its saying getServletContext() is undefined. Is this now depreciated or is some set missing?
Thanks Mkyong! This was exactly what I was looking for.
Generally speaking, without parsing the web.xml yourself, you can’t. The web.xml is parsed by the servlet container, so you can’t access it without it being read by the servlet container. There are, however, a few ways of getting around this.
Essentially you need the servlet container to be available at some point. Once it is (say in the init of a filter or something) you can save the value off in your pojo or in a place where your pojo can get to it. Like a static utility class or something. Then you pojo could just get the value from there. Likewise, if your pojo would be used from WITHIN a servlet context, you could store the current ServletConfig on a thread local so that your pojo could access it. Just be sure to clean it up later.
I’m not sure what your doing here or why you would want to access the web.xml properties outside of a web container, but these are all options for retrieving the information.
Of course, you can always parse the web.xml directly. It is XML and have a very well-defined schema.
hello there and thank you for your information – I’ve certainly picked up anything new from right here. I did however expertise a few technical points using this site, as I experienced to reload the site lots of times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your quality score if advertising and marketing with Adwords. Well I’m adding this RSS to my e-mail and could look out for a lot more of your respective exciting content. Ensure that you update this again soon..
I am looking for the same thing, accessing the context_param tag from a regular java class, not a ervlet. Have you found any answers on this?
what if we need to extract context-param inside plain java class file ie java class which is not filter, servlet.