Servlet Context Attribute Listener

ServletContextAttributeListener Interface for receiving notification events about ServletContext attribute changes.

In order to receive these notification events, the implementation class must be either declared in the deployment descriptor of the web application, annotated with WebListener, or registered via one of the addListener methods defined on ServletContext.

The order in which implementations of this interface are invoked is unspecified.

Method Summary
 void attributeAdded(ServletContextAttributeEvent event) receives notification that an attribute has been added to the ServletContext.
 void attributeRemoved(ServletContextAttributeEvent event) receives notification that an attribute has been removed from the ServletContext.
 void attributeReplaced(ServletContextAttributeEvent event) receives notification that an attribute has been replaced in the ServletContext.

Step 1 Create New Dynamic Web Project

Step 1.1 Create New Project

Go to File->New->Others… Select Dynamic Web Project under Web category then click Next. Type ServletDemo or whatever name you want in the Project name field.

Step 1.2 Configure Target Runtime

  • Click New Runtime… button under Target Runtime.
  • Select Apache Tomcat v7.0. Click Next.
  • Click Browse button to specify Tomcat Server installation directory. The select the folder then click Ok.
  • Click Installed JREs… button to add/remove or edit JRE definition. Remove existing one; then click Add button to configure new one. Select Standard VM; then click Next. Click Directory button to select the root folder of JDK installation. Select the folder then click Ok. Click Finish.
  • Check the JDK then click Ok.
  • Change JRE to the JDK then click Finish.
  • Click Finish the finish the project creation.

Step 2 The Listener Class

Create new java class that implements javax.servlet.ServletContextAttributeListener interface.

  • Right click to the project or src folder, select New -> Class from the menu.
  • In Java package, enter info.java.tips.config.
  • In Class Name, enter ServletContextAttributeListenerDemo or  any name you want.
  • Click Finish.
  • Add implements ServletContextAttributeListener to the class. You have to implement all the methods of the interface.

Step 3 Create New Servlet Class

Create new java class that extends javax.servlet.HttpServlet class.

  • Right click to the project or src folder, select New -> Class from the menu.
  • In Java package, enter info.java.tips.servlet.
  • In Class Name, type HttpServletListenerDemo or any name you want.
  • Click Finish.
  • Add extends HttpServlet to the class.
  • Implement doGet() method.

Step 4 Configuring web.xml

The web.xml file is located in the WEB-INF directory of your Web application.

The first entry, under the root servlet element in web.xml, defines a name for the servlet and specifies the compiled class that executes the servlet. The servlet element also contains definitions for initialization attributes and security roles for the servlet.

Step 4.1 Configure The Servlet

The second entry in web.xml, under the servlet-mapping element, defines the URL pattern that calls this servlet.

For each servlet, servlet-name must be the same for <servlet> & <servlet-mapping> elements.

Step 4.2 Configure The Listener

The listener class is also registed in web.xml.

For each servlet, servlet-name must be the same for <servlet> & <servlet-mapping> elements.

Step 5 Clean & Build

Clean & build the project to clean old classes file (if any) & force the IDE to recompile java code.

  • Go to Project on menu bar; select Clean…
  • Select Clean all projects (default)
  • Click Ok to proceed clean & build.

Step 6 Configure Tomcat

Select Servers window; in case it’s not there, go to Windows -> Show View-> Others… Select Server -> Servers; then click Ok.

If this is the first time you are creating a server, in the Servers area you will see a link as No servers are available click this link to create a new server. Click the link.

Define a New Server window will be opened, In Select the server type. Select Apache -> Tomcat v7.0 Server. Then click Finish.

Step 7 Deploy The Application

  • Right click to the Tomcat Server select Add and Remove.
  • Move the application (ServletDemo) from Available column to Configured. Then click on Finish to deploy it to tomcat server.

Step 8 Start Tomcat Server

Right click on the tomcat server and choose Start or simply click on the green start icon to start the server.

Step 9 Run The Servlet

We assume that you install tomcat on default port (8080) and project name is ServletDemo. Open web browser and type:

http://localhost:8080/ServletDemo/httpServletListenerDemo

Output

Everything in the steps & output is basically the same as in previous lesson. But take a look at the console windows (highlighted lines), you will see some event happens:

Note: If you’ve already created a dynamic web project & tomcat server up & running. You can skip step 1 – create new project. And to deploy & run the servlet; follow the below steps:

  • Stop the tomcat server
  • Clean & build the project to re-compile the application
  • Start the tomcat server
  • Open web browser and run the URL.