Engineering Full Stack Apps with Java and JavaScript
In this demo, we will create a listener and then add, remove and delete attributes from the request object so that the listener methods are invoked.
Follow the below steps in order. You should have already configure apache tomcat with eclipse as mentioned in configuring-apache-tomcat-with-eclipse.
Create a new Dynamic Web project in eclipse as:
go to File >New > Dynamic Web Project
Give 'Project name' and click next
Leave the default output folder as it is and click next
Check 'Generate web.xml deployment descriptor' and click 'Finish'
Create a servlet as:
Navigate to the project created
Right click the project name on 'Project Explorer' and select New > Servlet
In the 'Create Servlet' window, give a package name. Eg: com.javajee.servlets
Give a class name
By default, superclass of servlet will be 'javax.servlet.http.HttpServlet' an click finish
Create a listener class from eclipse as:
Right click the project and click New > Listener
In 'Create Listener' window, give a package name. Eg:com.javajee.listeners
Give a class name, say MyAttributeListener, and click Next
Select the application lifecycle events to listen to
Check 'Changes to Attributes (javax.servlet.ServletRequestAttributeListener)' under 'Servlet request events'
Click Finish.
The listener created by eclipse will be annotated using @WebListener annotation.
Alternatively, you can configure the listener class in web.xml as:
<listener>
<listener-class>com.javajee.listener. MyAttributeListener</listener-class>
</listener>
Configuring both in web.xml and using annotations will not give any error, however only one of these is required.
Use below print statements inside listener methods to know when those methods are been called:
public void attributeRemoved(ServletRequestAttributeEvent arg0) {
System.out.println("attributeRemoved. Name="+arg0.getName()+". Value="+arg0.getValue());
}
public void attributeAdded(ServletRequestAttributeEvent arg0) {
System.out.println("attributeAdded. Name="+arg0.getName()+". Value="+arg0.getValue());
}
public void attributeReplaced(ServletRequestAttributeEvent arg0) {
System.out.println("attributeReplaced. Name="+arg0.getName()+". Value="+arg0.getValue());
}
Now write code in the doGet method of the servlet to add, replace and remove attributes:
request.setAttribute("myname", "sneha");
request.setAttribute("myname", "snehaheartin");
request.removeAttribute("myname");
Listener responds to the changes made to the attributes attached to the request object and prints below in the console:
attributeAdded. Name=myname. Value=sneha
attributeReplaced. Name=myname. Value=sneha
attributeRemoved. Name=myname. Value=snehaheartin
If you misspell your listener class in web.xml, you will get an exception like:
java.lang.ClassNotFoundException: com.javajee.listeners.MyAttributeListeners
So we should be very careful when using web.xml to avoid such typos. It would be a better choice to use annotations as it will be verified by the compiler.