ServletRequestListener

This Listeners listens the life cycle events of ServletRequest object,like creation and destruction.
This interface defines the following two methods.

  1. public void requestInitialized(ServletRequestEvent e)
  2. This method invoked by webContainer at the time of Request object creation,i.e it will be executed just before starting service() method.

  3. public void requestDestroyed(ServletRequestEvent e)
  4. This method will be executed automatically by the web container at the time of request destruction or just after completing service method.

ServletRequestEvent

ServletRequestEvent is the child class of java.util.EventObject.It contains only one method getSource() which returns the source of the Event.In this case the source is web application Which is represented by ContextObject.
ServletRequestEvent represents event related to request object .This class contains the following two utility methods.

  1. public ServletRequest getServletRequest()
  2. public ServletContext getServletContext()

RequestListener

Example: To Print hit count of the web application.

  1. Create ServletRequestListener implemented class
  2. Create FirstServlet class
  3. Configure ServletRequestListener & FirstServlet class in web.xml
  4. Deploy application & Run

Create ServletRequestListener implemented class

package com.java2learn.listener;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

public class RequestListenerDemo implements ServletRequestListener {

	public static int count = 0;

	public void requestInitialized(ServletRequestEvent e) {
		count++;
		System.out
				.println("Request Object created at::" + new java.util.Date());
		System.out.println("Hit Count for <a class="zem_slink" href="http://en.wikipedia.org/wiki/Web_application" title="Web application" rel="wikipedia" target="_blank">Web Application</a>::" + count);

	}

	public void requestDestroyed(ServletRequestEvent e) {
		System.out.println("Request Object destroyed at::"
				+ new java.util.Date());

	}

}

Create FirstServlet class

package com.java2learn.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.java2learn.listener.RequestListenerDemo;

public class FirstServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

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

		PrintWriter out=response.getWriter();
		out.println("RequestListenerDemo");
		System.out.println("Hit Count for Web Application::" + RequestListenerDemo.count);
	}
}

Configure ServletRequestListener & FirstServlet class in web.xml

<?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>ListenersDemo</display-name>
	<servlet>
		<description></description>
		<display-name>FirstServlet</display-name>
		<servlet-name>FirstServlet</servlet-name>
		<servlet-class>com.java2learn.servlet.FirstServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>FirstServlet</servlet-name>
		<url-pattern>/FirstServlet</url-pattern>
	</servlet-mapping>

	<listener>
		<listener-class>com.java2learn.listener.RequestListenerDemo</listener-class>
	</listener>
</web-app>

Deploy application & Run

OutputRequestListener

Final Project Structure:
PackageStructureListenerDemo

Download Project: ListenersDemo

Configuring a Listener in web.xml

We can configure a listener by using tag.<listener> tag is the direct child tag of <web-app> and we can take any where within <web-app>

</web-app>
  <listener>
	<listener-class>com.java2learn.listener.RequestListenerDemo</listener-class>
 </listener>
</web-app>

Note:

  1. We can configure any number of same type of listeners but the order of listeners execution depends upon the order of <listener> tags in web.xml.
  2. Web container is responsible for the creation of listener class objects for this.Web container always calls public no-arg constructor.Hence every listener class should contain public no-arg constructor.
  3. Instantiation of the listener happens at the time of Server start up or at the time of application deployment.

ServletRequestAttributeListener

This Listener listens the events related requestScoped attributes like attribute addition/removal.This interface defines the following 3 methods.

  1. public void attributeAdded(ServletRequestAttributeEvent e)
  2. This method will be executed automatically by the web container when ever we are adding an attribute in the request scope.

  3. public void attributeRemoved(ServletRequestAttributeEvent e)
  4. public void attributeReplaced(ServletRequestAttributeEvent e)
ServletRequestAttributeEvent

This class contains the following two methods

  1. public String getName();
  2. Returns the name of the attribute,which has added/replaced/removed

  3. public object getValue();
    Returns the value of the attribute,which has added/removed

In the case of replacement this method will return old object.
ServletRequestAttributeEvent is the child class of ServletRequestEvent.

ServletRequestAttributeEvent

Example:

  1. Create a class RequestAttributeListenerDemo implementing ServletRequestAttributeListener interface.
  2. Create FirstServlet class
  3. Configure RequestAttributeListenerDemo & FirstServlet in web.xml
  4. Deploy & Run the application

Create a class RequestAttributeListenerDemo implementing ServletRequestAttributeListener interface.

package com.java2learn.listener;

import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;

public class RequestAttributeListenerDemo implements
		ServletRequestAttributeListener {

	public void attributeAdded(ServletRequestAttributeEvent e) {
		System.out.println(e.getName() + "::Attribute is added");

	}

	public void attributeRemoved(ServletRequestAttributeEvent e) {
		System.out.println(e.getName() + "::Attribute is removed");

	}

	public void attributeReplaced(ServletRequestAttributeEvent e) {
		System.out.println(e.getName() + "::Attribute is replaced");

	}
}

Create FirstServlet class

package com.java2learn.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

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

		request.setAttribute("JAVA","Java Course");
		request.setAttribute("PHP","PHP Course");
		request.removeAttribute("PHP");
		request.setAttribute("JAVA","JEE");
		System.out.println("---This is RequestAttributeListenerDemo---");
	}
}

Configure RequestAttributeListenerDemo & FirstServlet in web.xml

<?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>ListenersDemo</display-name>
	<servlet>
		<description></description>
		<display-name>FirstServlet</display-name>
		<servlet-name>FirstServlet</servlet-name>
		<servlet-class>com.java2learn.servlet.FirstServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>FirstServlet</servlet-name>
		<url-pattern>/FirstServlet</url-pattern>
	</servlet-mapping>

	<listener>
		<listener-class>com.java2learn.listener.RequestAttributeListenerDemo</listener-class>
	</listener>
</web-app>

Deploy & Run the application

RequestAttributeOutput

Package Structure:
RequestAttributePackage

Download Project: ListenersDemo