• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet properties file

 
Joshua Fix
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to be able to read some properties from a file in my servlet. I do not expect these properties to change very often at all, so I don't want to read the file with each request. I've decided to use a context listener so that the file is read in only one time when the servlet loads. From the context listener, what is the best way to make those properties accessible to the servlet class? Should it just be a static properties object in the listener class? I would like to keep it as simple as possible.

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I place all such information in application context (via servletContext.setAttribute()).
 
Joshua Fix
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you loop through the properties and set each attribute to each property individually, or do you store the entire Properties object as one attribute?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends. Frequently I'll store the data as a Map if easy retrieval from a JSP using EL is desired.
 
suri jagadish
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You can load the properties file just like this and store the properties object as the context attribute. Now retrieve the properties object into your servlet and then get your property as props.getProperty("") thats all. so that you don't need to loop thorough to set the properties.

InputStream inputStream = getServletContext().getResourceAsStream("/WEB-INF/app.properties");
Properties props = new Properties();
props.load(inputStream);
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, we've already established that. Note that because Properties implements Map, the properties will be easily available to the EL.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic