• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

servlet reading properties file

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to get my servlet to load a properties file in the servlets init() method.
However the servlet complains that it cannot find the file. Does anyone know what directory the file should be in please? I am running tomcat, my web app is in a directory called 'helpdesk'. Many thanks...
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to change ur web.xml to look like this
<servlet>
<servlet-name>xyz</servlet-name>
<servlet-class>xyzServlet</servlet-class>
<init-param>
<param-name>properties</param-name>
<param-value>/WEB-INF/conf/helpdesk.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

put ur properties file at web-inf\conf\ directory.
Later in ur servlet u can read the init parameter like this.
String filename = getServletContext().getInitParameter("properties");

Hope this hlep
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the following an alternative for the same?Please clarify:
Properties prop = new Properties();
prop.load(<FileInputStream> ;
and then reading the values using the usual
looping
Regards
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below is from production code I have running at works fine. The object sc is the ServletContext.

As in the example above, put the name of the .properties file as a parameter. I am using a ServletContextListener, so my parameter must be a context parameter. However, you could easily use a servlet with a servlet parameter.
Use the ServletContext.getResourceAsStream method as this allows the file to be found regardless of deploy type (.war or non-jar). Since this returns a stream and a properties object can load from a stream, it is a good match.
 
Saloon Keeper
Posts: 25777
184
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The usual problem people have is because relative file paths don't work in a servlet environment.
However, it you use the Java Properties services, you can read the properties simply by naming them as though they were a Java class (which they might be anyway!).
for example, "com.mywebsite.myapp.Global" is a valid name for the resource contained in the file "WEB-INF/classes/com/mywebsite/myapp/Global.properties".
 
Ken Robinson
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example I have listed works. As stated, it is in a production environment. Using the method you have just listed seems to have limitations. I would suggest leveraging what the system is required to do and what the Properties object, by design, provides to you.
While the getRealPath method has issues with getting a stream of a resource, I have never had an issue with getting a file using getResourceAsStream.
Think about it, packages such as STRUTS and Log4J must be able to get to their config files, which are usually stored under the WEB-INF directory. A relative path is supposed to work in the current context. If it does not, I doubt the product would have passed J2EE certification.
 
reply
    Bookmark Topic Watch Topic
  • New Topic