This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet properties file" Watch "Servlet properties file" New topic
Author

Servlet properties file

Joshua Fix
Ranch Hand

Joined: Sep 18, 2007
Posts: 57
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!


SCJP 5.0
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

I place all such information in application context (via servletContext.setAttribute()).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joshua Fix
Ranch Hand

Joined: Sep 18, 2007
Posts: 57
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

Joined: Jan 10, 2002
Posts: 60738
    
  65

Depends. Frequently I'll store the data as a Map if easy retrieval from a JSP using EL is desired.
suri jagadish
Greenhorn

Joined: Sep 14, 2005
Posts: 19
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

Joined: Jan 10, 2002
Posts: 60738
    
  65

Yes, we've already established that. Note that because Properties implements Map, the properties will be easily available to the EL.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet properties file
 
Similar Threads
Servlet config in servlets
Detect if a .properties file is in a jar
How to deal with configuration data in a (web) application?
properties file, default location
How can a non-servlet find application root and best way to access a servlets data?