This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Loading property file values to a hash map

 
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.
 
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
 
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
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
    Bookmark Topic Watch Topic
  • New Topic