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>mkyong2002@yahoo.com</param-value>
</context-param>

We can get the above “AdministratorEmail” context-param value with the following java code.


String email= getServletContext().getInitParameter("AdministratorEmail");

Result


mkyong2002@yahoo.com

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
Peter RangelovScott O'BryanSuanne TebayKeithbahadur baniya Recent comment authors
newest oldest most voted
Peter Rangelov
Guest
Peter Rangelov

Thanks Mkyong! This was exactly what I was looking for.

Scott O'Bryan
Guest
Scott O'Bryan

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… Read more »

Suanne Tebay
Guest
Suanne Tebay

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… Read more »

Keith
Guest
Keith

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?

bahadur baniya
Guest
bahadur baniya

what if we need to extract context-param inside plain java class file ie java class which is not filter, servlet.