In a class

class MyServletContextListener implements ServletContextListener //line 1
{         
         public void contextInitialised(ServletContextEvent event){
              ServletContext sc = event.getServletContext(); //line 2
              //other functionalities
         }
}

I want to ask in "line 2" how is event variable being used to invoke a method getServletContext without declaring an object of ServletContextEvent class? Here ServletContextEvent event = new ServletContextEvent(); is not being done here. Can we do so for any general classes?

1

There are 1 best solutions below

0
On BEST ANSWER

This is a class that is instantiated and executed by servlet runtime (tomcat/jetty). Its part of the Servlet API Specificate. You can read about this here : https://docs.oracle.com/cd/B14099_19/web.1012/b14017/filters.htm

Creation of ServletContextEvent object is taken care by the servlet runtime. And it passes that object in the invocation of the method.