getResourceAsStream contextInitialized

258 views Asked by At

I am writing a web application in Java using Tomcat as servlet container. I wrote a AppLoader class implementing ServletContextListener to trap the event contextInitialized and perform some init operations. No problem about that, but when I need to read the content of a resource (txt file) using the getResourceAsStream method, it fails and returns null. I noticed that the same operation run in a Servlet service (so, after the web app init event) works fine and returns the file content.

It seems I can't access internal resources until the app is loaded.

How can I solve? Is there a "ready" event to trap for a web app?

Thank you for any suggestion

1

There are 1 answers

2
Vijay On

you can also do it by loading a servlet with load on Statup automatically , and call getResourceAsStream() method in init of servlet like this

<servlet>
   <servlet-name>StartupServlet</servlet-name>
   <display-name>StartupServlet</display-name>
   <servlet-class>com.myApp.StartupServlet</servlet-class>
   <load-on-startup>0</load-on-startup>
 </servlet> 

In Servlet

public class StartupServlet extends HttpServlet{
@override
public void init()
{
       // do you operation here.
}

@override
public void doPost(HttpServletRequest req,HttpServletResponse res)
}