4
I have the following declared Servlet on web.xml and would like it to be initialized before any of the other application Serrvlets:
<servlet>
<servlet-name>ListarFilmesIndex</servlet-name>
<servlet-class>br.com.corporacao.servlets.ListarFilmesIndex</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ListarFilmesIndex</servlet-name>
<url-pattern>/ListarFilmesIndex</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
br.com.corporacao.servlets.InicializacaoServletContextListener
</listener-class>
</listener>
I tried to implement the class you use ServletContextListener
:
package br.com.corporacao.servlets;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class InicializacaoServletContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent e) {
}
public void contextDestroyed(ServletContextEvent e) {
}
}
But I can’t instantiate within the method contextInitialized
the Servlet ListarFilmesIndex
that uses a DAO to upload all movies that are registered in the database.
When I try to instantiate the class ListarFilmesIndex
I don’t know what to do with the request and the sponse which are required for Servlet, and then when I run the application a NullPointerException
.
I have tried everything and searched but found nothing, only examples of filters and classes that are not Servlets.
What should I do?