|
Page 3 of 5
Implementation of ServletContextListener
As you may have known, ServletContextListener is useful if you would like to initialize your web application with some values in the beginning of your web application. Okay, without wasting any more time, let’s start our first Listener which is ServletContextListener.
Right click on the src folder (this is your source folder) and choose New and Class. Although Listener is part of Servlet, we can simply create it as a simple Java class. Subsequently, we need to customize it to be a Listener. Do not worry; it would not be too hard. Please follow the tutorial and you should see how we convert this simple Java class to Listener.
In this wizard, you may define the package for your Java class. Normally, we use Package to group a few Java files that has the same functionalities. For example, if I have StringUtil.java for String manipulation and EncodingAlgorithmUtil.java for encoding, I will locate them under the same package called “com.mycompanyname.util” as they both are actually utility classes. So as this is our first Listener, let’s create one package called “com.mycompany.listener”. The class name is basically your Listener name. To make it self-explanatory, I name it ServletListener. You can leave other configuration as they are.
Once it is completed, it should look like below.
Now, the most important thing is that we need to make the class “implements” the ServletContextListener. So here is my latest ServletListener.java.
|
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.