I have a servlet that reads in a .properties file on init(). My code (not the one below) works if I have a context-parameter set in my web.xml but I read that a context-parameter is globally accessible and I don't want that as this servlet is just a piece of a bigger web application. I just want to be able to do this using the init-param tag I tried this:
public void init(ServletConfig config) throws ServletException {
try {
String fileName = config.getInitParameter("configFile");
System.out.println(fileName);
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
p = new Properties();
p.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
}
but I keep getting file not found exception. I have searched the internet but most people use servlet contexts. How else can I load my properties file without including the context-param tag in my web.xml?
Thanks!
EDIT:
java.io.FileNotFoundException: C:\WEB-INF\classes\myapp.properties (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at ipadService.ProxyServlet.init(ProxyServlet.java:53)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1206)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
This is an element within the servlet. The optional init-param element contains a name/value pair as an initialization parameter of the servlet. Use a separate set of init-param tags for each parameter. You can access these parameters with the javax.
The “context-param” tag is define in “web. xml” file and it provides parameters to the entire web application. For example, store administrator's email address in “context-param” parameter to send errors notification from our web application.
init. Called by the servlet container to indicate to a servlet that the servlet is being placed into service. The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests.
The init-param element within a filter or servlet definition element contains initialization parameters for that filter or servlet instance. These are distinct from context parameters, discussed in Section 4.7. Each init-param contains a param-name element and a param-value element.
Given that fileName
is /WEB-INF/classes/myapp.properties
, you need to get it as a webapp resource, not as a local disk file system file.
So, replace
String fileName = config.getInitParameter("configFile");
System.out.println(fileName);
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
p = new Properties();
p.load(fis);
by
String fileName = config.getInitParameter("configFile");
InputStream input = config.getServletContext().getResourceAsStream(fileName);
p = new Properties();
p.load(input);
A simpler way is to set the fileName
to myapp.properties
and get it as a classpath resource.
String fileName = config.getInitParameter("configFile");
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
p = new Properties();
p.load(input);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With