Creating servlet that implements contextInitializer interface in this code,
then accessing file inside contextinitialized() using this line
InputStream input = getServletContext().getResourceAsStream("/WEB-INF/file.properties"));
this exception occurred
java.lang.NullPointerException at
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:160)
any ideas ?
The
ServletContextListener#contextInitialized()gives you theServletContextEventargument which provides you thegetServletContext()method.Thus, this should do:
That said, you normally don’t want your servlet to implement this interface. The listener has a different purpose. Just override the
HttpServlet#init()as follows: