Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading property file values to a hash map

 
Jayanath Amaranayake
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Python Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64606
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you doing all these gymnastics when the JSTL <fmt:message> tag will do it for you?
 
Jayanath Amaranayake
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic