This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Servlets and the fly likes Loading property file values to a hash map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Loading property file values to a hash map" Watch "Loading property file values to a hash map" New topic

Loading property file values to a hash map

Jayanath Amaranayake

Joined: Dec 06, 2009
Posts: 7
Hi all,
I have few jsp pages that need to display some selected values (key value pair) from a properties file at each page load. I thought I'm going to try something like below.
1- Have a servlet with overridden init method to read the file and load the key value pairs in to a hash map
2- Access the map from each page.
The problem is I couldn't figure out how to share the map to the jsp pages.

If I use a java class to load the key value map and return values when needed, I will have to read the property file, each time I create an instance of that class.

Thanks in advance for any suggestions / help to resolve this problem.
Kelvin Tan

Joined: May 02, 2009
Posts: 10

Can the properties be loaded at startup time?

This way you can store these key-values into the ServletContext, every servlet can take it off the ServletContext and passing these values to the jsps.

I maybe wrong here, but I remember reading this in the Heads First book.

I hope I may be of help.

Patel Chintan
Ranch Hand

Joined: Mar 01, 2007
Posts: 80
Yes Kelvin is right. You can store your map as an attribute in servletcontext.
And sorvletcontext is accessible to each JSP, so you can use it. and if property is going to change per request then you can set map as attribute in session object.

-Chintan Patel

Chintan Patel, SCJP1.5,
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63542

Why are you doing all these gymnastics when the JSTL <fmt:message> tag will do it for you?

[Asking smart questions] [About Bear] [Books by Bear]
Jayanath Amaranayake

Joined: Dec 06, 2009
Posts: 7
Thanks for all the replies. I'm going to use the ServletContext to hold my map and that prevents me loading the map again and again.
Bear, I looked in to <fmt:message> tag, but couldnt figure out how it would help me to solve the same problem. If you could elaborate it a little bit, that would be great.

Thanks again for your time.
I agree. Here's the link:
subject: Loading property file values to a hash map
It's not a secret anymore!