ServletContextEvent and ServletContextListenerThe ServletContextEvent is notified when web application is deployed on the server. If you want to perform some action at the time of deploying the web application such as creating database connection, creating all the tables of the project etc, you need to implement ServletContextListener interface and provide the implementation of its methods. Constructor of ServletContextEvent classThere is only one constructor defined in the ServletContextEvent class. The web container creates the instance of ServletContextEvent after the ServletContext instance.
Method of ServletContextEvent classThere is only one method defined in the ServletContextEvent class:
Methods of ServletContextListener interfaceThere are two methods declared in the ServletContextListener interface which must be implemented by the servlet programmer to perform some action such as creating database connection etc.
Example of ServletContextEvent and ServletContextListenerIn this example, we are retrieving the data from the emp32 table. To serve this, we have created the connection object in the listener class and used the connection object in the servlet. index.htmlMyListener.javaMyListener.javaExample of ServletContextListener to create table of a projectIn this example, we are creating table of the project. So we don't need to create all the tables manually in the database. MyListener.javaOther Examples of ServletContextListenerExample of ServletContextListener to improve performance |