In my web application I have to send email to set of predefined users like finance@xyz.example, 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.
In my web application I have to send email to set of predefined users
Share
It’s your choice. There are basically three ways in a Java web application archive (WAR):
1. Put it in classpath
So that you can load it by
ClassLoader#getResourceAsStream()with a classpath-relative path:Here
foo.propertiesis supposed to be placed in one of the roots which are covered by the default classpath of a webapp, e.g. webapp’s/WEB-INF/liband/WEB-INF/classes, server’s/lib, or JDK/JRE’s/lib. If the propertiesfile is webapp-specific, best is to place it in/WEB-INF/classes. If you’re developing a standard WAR project in an IDE, drop it insrcfolder (the project’s source folder). If you’re using a Maven project, drop it in/main/resourcesfolder.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.loaderproperty ofTomcat/conf/catalina.properties.If you have placed the
foo.propertiesit in a Java package structure likecom.example, then you need to load it as belowNote that this path of a context class loader should not start with a
/. Only when you’re using a "relative" class loader such asSomeClass.class.getClassLoader(), then you indeed need to start it with a/.However, the visibility of the properties file depends then on the class loader in question. It’s only visible to the same class loader as the one which loaded the class. So, if the class is loaded by e.g. server common classloader instead of webapp classloader, and the properties file is inside webapp itself, then it’s invisible. The context class loader is your safest bet so you can place the properties file "everywhere" in the classpath and/or you intend to be able to override a server-provided one from the webapp on.
2. Put it in webcontent
So that you can load it by
ServletContext#getResourceAsStream()with a webcontent-relative path:Note that I have demonstrated to place the file in
/WEB-INFfolder, otherwise it would have been public accessible by any webbrowser. Also note that theServletContextis in anyHttpServletclass just accessible by the inheritedGenericServlet#getServletContext()and inFilterbyFilterConfig#getServletContext(). In case you’re not in a servlet class, it’s usually just injectable via@Inject.3. Put it in local disk file system
So that you can load it the usual
java.ioway with an absolute local disk file system path:Note the importance of using an absolute path. Relative local disk file system paths are an absolute no-go in a Java EE web application. See also the first "See also" link below.
Which to choose?
Just weigh the advantages/disadvantages in your own opinion of maintainability.
If the properties files are "static" and never needs to change during runtime, then you could keep them in the WAR.
If you prefer being able to edit properties files from outside the web application without the need to rebuild and redeploy the WAR every time, then put it in the classpath outside the project (if necessary add the directory to the classpath).
If you prefer being able to edit properties files programmatically from inside the web application using
Properties#store()method, put it outside the web application. As theProperties#store()requires aWriter, you can’t go around using a disk file system path. That path can in turn be passed to the web application as a VM argument or system property. As a precaution, never usegetRealPath(). All changes in deploy folder will get lost on a redeploy for the simple reason that the changes are not reflected back in original WAR file.See also: