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.
views:
1473answers:
3It 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).
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
It's your choice. There are basically three ways:
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
orJRE/lib
. If the propertiesfile is webapp-specific, best is to place it inWEB-INF/classes
. If you're developing a project in an IDE, you can also drop it insrc
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 ofTomcat/conf/catalina.properties
.Put it somewhere in
web
folder (the project's web content folder), so that you can load it byServletContext#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 theServletContext
is in anyHttpServlet
class just accessible by the inheritedGenericServlet#getServletContext()
.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.