Home Java Javaee6 @WebListener Annotation Servlet Example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

@WebListener Annotation Servlet Example

Advertisement
In this tutorial you will learn what the difference is in the implementation of listener in servlet in older version and servlet 3.0 (Java EE6). The example given below will demonstrate you how to implement a listener interface in servlet.

@WebListener Annotation Servlet Example

In this tutorial you will learn what the difference is in the implementation of listener in servlet in older version and servlet 3.0 (Java EE6). The example given below will demonstrate you how to implement a listener interface in servlet.

In this example I will use @WebListener annotation to register a listener class to use with servlet. Here one thing you will have to remember that your annotated listener class must implement one or more than one of the following interfaces :

  • HttpSessionAttributeListener
  • HttpSessionListener
  • ServletContextAttributeListener
  • ServletContextListener
  • ServletRequestAttributeListener
  • ServletRequestListener

Here I have implemented the two interfaces in my listener class :

  1. HttpSessionListener : This interface receives the notification of the event changes in HttpSession life cycle. It has two methods :
    • sessionCreated(HttpSessionEvent se) : Notified when a session is created.
    • sessionDestroyed(HttpSessionEvent se) : Notified when a session is invalidated.
  2. HttpSessionAttributeListener : This interface receives the notification of the event changes in the HttpSession attribute. It has three methods :
    • attributeAdded(HttpSessionBindingEvent event) : Notified when an attribute adds to a session.
    • attributeRemoved(HttpSessionBindingEvent event) : Notified when an attribute is removed from a session.
    • attributeReplaced(HttpSessionBindingEvent event) : Notified when an attribute has been replaced in a session.

Example :

ListenerTester.java

package roseindia.net;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name= "ListenerTester", urlPatterns= "/listener")
public class ListenerTester extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session= request.getSession();
out.println("<h3>This is a simple example of @WebServlet with @WebListener</h3>");
session.setAttribute("info", "My Info");
String s= (String)session.getAttribute("info");
out.println("<br>Attribute value that you have set = "+s);
session.setAttribute("info", "Your Info");
String s1= (String)session.getAttribute("info");
out.println("<br>Attribute value has replaced by = "+s1);
session.removeAttribute("info") ;
session.invalidate();
} 

public void doPost(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
} 
}

WebListenerExample.java

package roseindia.net;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener()
public class WebListenerExample implements HttpSessionListener, HttpSessionAttributeListener
{

public void sessionCreated(HttpSessionEvent he) {
System.out.println("Session is created");
}

public void sessionDestroyed(HttpSessionEvent he) {
System.out.println("Session is destroyed"); 
}
public void attributeAdded(HttpSessionBindingEvent arg0) 
{

System.out.println("value is added");
}

public void attributeRemoved(HttpSessionBindingEvent arg0) 
{
System.out.println("value is removed");

}

public void attributeReplaced(HttpSessionBindingEvent arg0) 
{
System.out.println("value has been replaced");

}
}

Here in the web.xml file <listener></listener> entry is not required to give additionally but in the older version you were the bound to map this entry into web.xml file. The entry were given as :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>servletAnnotationExample</display-name>
<listener>
<listener-class>roseindia.net.WebListenerExample</listener-class>
</listener>
<servlet>
<servlet-name>ListenerTester</servlet-name>
<servlet-class>roseindia.net.ListenerTester</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListenerTester</servlet-name>
<url-pattern>/listener</url-pattern>
</servlet-mapping>
</web-app>

Output :

When you will execute the above example you will get the output as :

And on the console following message will be displayed as :

Download Source Code

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 14, 2011

Ask Questions?    Discuss: @WebListener Annotation Servlet Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com