aspose file tools*
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
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
Author

Loading property file values to a hash map

Jayanath Amaranayake
Greenhorn

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
Greenhorn

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.

Cheers
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, http://datewithjava.blogspot.com
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jayanath Amaranayake
Greenhorn

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.
 
Consider Paul's rocket mass heater.
 
subject: Loading property file values to a hash map
 
Similar Threads
Multiple set of data mapping
struts tags
Action form - design issue
Action form that contains a collection of an unkown length
Difference Between hashmap and Property class