2 Minutes To Code (β Version)

Java Servlets - 2 minute tutorial

It's time to write your first listener

Let's write our first http listener. We will write a ServletRequestListener. For any of the other 7 types, described in the previous page, the steps are analogous.

Our listener will need to implement the javax.servlet.ServletRequestListener interface as shown in the example below and implement the methods requestInitialized(..) and requestDestroyed(..) wherein we will write the required logic.

package com.techfundaes.servletsBag;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

public class MyServletListener implements ServletRequestListener
	public void requestInitialized(ServletRequestEvent servletRequestEvent)
		HttpServletRequest request = (HttpServletRequest)servletRequestEvent.getServletRequest();
		ServletContext servletContext = servletRequestEvent.getServletContext();
	public void requestDestroyed(ServletRequestEvent servletRequestEvent)