views:

1473

answers:

3

In my web application I have to send email to set of predefined users like [email protected], so I wish to add that to a .properties file and access it when required. Is this a correct procedure, if so then where should I place this file, I am using Netbeans IDE which is having two separate folders for source and JSP files, advice solicited.

A: 

It just needs to be in the classpath (aka make sure it ends up under /WEB-INF/classes in the .war as part of the build).

Taylor Leese
hi thanks for the idea, but it tells me that cannot find the file specified, yes its the path problem how to give the path
sansknwoledge
A: 

You can you with your source folder so whenever you build, those files are automatically copied to the classes directory.

Instead of using properties file, use XML file.

If the data is too small, you can even use web.xml for accessing the properties.

Please note that any of these approach will require app server restart for changes to be reflected.

Kalpak

Kalpak
i placed in the webpages folder but unable to access it file not found error is coming how to set path
sansknwoledge
if your file ends up in WEB-INF/classes folder, it automatically is set into the classpath
Kalpak
+3  A: 

It's your choice. There are basically three ways:

  1. Put it in the classpath, so that you can load it by ClassLoader#getResourceAsStream() with a classpath-relative path:

    Properties properties = new Properties();
    properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));
    

    Here filename.properties is supposed to be placed in one of the roots which are covered by the default classpath of a webapp, e.g. Webapp/WEB-INF/lib, Webapp/WEB-INF/classes, Appserver/lib or JRE/lib. If the propertiesfile is webapp-specific, best is to place it in WEB-INF/classes. If you're developing a project in an IDE, you can also drop it in src folder (the project's source folder).

    You can alternatively also put it somewhere outside the default classpath and add its path to the classpath of the appserver. In for example Tomcat you can configure it as shared.loader property of Tomcat/conf/catalina.properties.

  2. Put it somewhere in web folder (the project's web content folder), so that you can load it by ServletContext#getResourceAsStream() with a webcontent-relative path:

    Properties properties = new Properties();
    properties.load(getServletContext().getResourceAsStream("/WEB-INF/filename.properties"));
    

    Note that I have demonstrated to place the file in /WEB-INF folder, otherwise it would have been public accessible by any webbrowser. Also note that the ServletContext is in any HttpServlet class just accessible by the inherited GenericServlet#getServletContext().

  3. Put it somewhere in local disk file system so that you can load it the usual java.io way with an absolute local disk file system path:

    Properties properties = new Properties();
    properties.load(new FileInputStream("/absolute/path/to/filename.properties");
    

Just outweigh the advantages/disadvantages in your own opinion of maintainability. I personally prefer putting it in the classpath outside the project (add new path to the classpath), so that I can manage it from outside and so I don't need to hardcode an absolute disk file system path in my Java code. Putting the file in the project itself would overwrite the file on every deploy and that's not useful if you intend to be able to modify the file programmatically using Properties#store() and so on. Putting the file outside the classpath in the local disk file system would require you to access it with a hardcoded path, which makes the code less portable.

BalusC
balusc you are my man, i wish to give you a title java superstar. ;-)
sansknwoledge
You're welcome. Just an upvote is also enough :)
BalusC