Every application deployed on the server has its own lifecycle. Which is known as ApplicationContext Lifecycle.
Application Context has two events / phases in a lifecycle,here are the main events of it:
1) context initialized
2) context destroyed
If we want to perform any action while context is initialized or context is destroyed then we can use "ServletContextListener".
To implement same here are the steps needs to be performed:
1) Modify web.xml (deployment descriptor) file to add listener
2) Add a class which implements "ServletContextListener"
3) Run and Verify
Lets see the steps in detailed:
1) Modify web.xml (deployment descriptor) file to add listener
web.xml file will be read / looked upon by the container while loading any of the project. To inform container about "ServletContextListener" we need to add below
lines in it:
<listener>
<listener-class>com.listener.ApplicationContextListener</listener-class>
</listener>
2) Add a class which implements "ServletContextListener"
Here is the code snippet for the quick reference:
public class ApplicationContextListener implements ServletContextListener {
public ApplicationContextListener() {
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Context Initialzied");
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("Context Destroyed");
}
}
As we can see there are two methods "contextInitialized" and "contextDestroyed" which will be called at the time of application being deployed and undeployed
respectively.
3) Run and Verify
When Server is getting up we will see below message
INFO: Starting service Catalina
Jul 10, 2015 3:15:11 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.35
Context Initialzied
While unloading "student" application we will see below message:
Jul 10, 2015 7:25:53 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/student] has started
Context Destroyed
Cheers!
Henal Saraiya
(Lead Consultant)
CIGNEX Datamatics
Application Context has two events / phases in a lifecycle,here are the main events of it:
1) context initialized
2) context destroyed
If we want to perform any action while context is initialized or context is destroyed then we can use "ServletContextListener".
To implement same here are the steps needs to be performed:
1) Modify web.xml (deployment descriptor) file to add listener
2) Add a class which implements "ServletContextListener"
3) Run and Verify
Lets see the steps in detailed:
1) Modify web.xml (deployment descriptor) file to add listener
web.xml file will be read / looked upon by the container while loading any of the project. To inform container about "ServletContextListener" we need to add below
lines in it:
<listener>
<listener-class>com.listener.ApplicationContextListener</listener-class>
</listener>
2) Add a class which implements "ServletContextListener"
Here is the code snippet for the quick reference:
public class ApplicationContextListener implements ServletContextListener {
public ApplicationContextListener() {
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Context Initialzied");
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("Context Destroyed");
}
}
As we can see there are two methods "contextInitialized" and "contextDestroyed" which will be called at the time of application being deployed and undeployed
respectively.
3) Run and Verify
When Server is getting up we will see below message
INFO: Starting service Catalina
Jul 10, 2015 3:15:11 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.35
Context Initialzied
While unloading "student" application we will see below message:
Jul 10, 2015 7:25:53 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/student] has started
Context Destroyed
Cheers!
Henal Saraiya
(Lead Consultant)
CIGNEX Datamatics
No comments:
Post a Comment