I have the following servlet declared in web.xml and would like it to be initialized before any of the other servlets of the application:
<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 that uses 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 not instantiate within the contextInitialized
method the servlet ListarFilmesIndex
that uses a DAO to load all the movies that are registered in the database.
When I try to instantiate the class ListarFilmesIndex
I do not know what to do with the request and the response that are required for the servlet, and then when I run the application it is NullPointerException
is released.
I have tried everything and searched but found nothing, just examples of filters and classes that are not servlets.
What should I do?