Searching...
Tuesday, 2 October 2012

Servlet Listner Examples

02:07

Event Listeners

The servlet specification includes the capability to track key events in your Web applications through event listeners. This functionality allows more efficient resource management and automated processing based on event status. Ref: Oracle.
 
In this example we are going to add three listeners like
 
HttpSessionListener

ServletContextListener

ServletRequestListener

Example

Session Listener
 
Create a listener class which implements HttpSessionListener interface and annotated with @Weblistner
 
package com.vinod.test;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Application Lifecycle Listener implementation class MySessionListener
 *
 */
/**
 *@authorvinodkariyathungalkumaran
 *
 */
@WebListener
public class MySessionListener implements HttpSessionListener {

@Override
public void sessionCreated(HttpSessionEvent arg0) {

System.out.println("Session created " + arg0.getSession().getId());
}

@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("Session destroyed");

}

}
 
Context Listener
 
package com.vinod.test;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * Application Lifecycle Listener implementation class ContextListener
 *
 */
/**
 *@authorvinodkariyathungalkumaran
 *
 */
@WebListener
public class ContextListener implements ServletContextListener {

/**
     * Default constructor.
     */
public ContextListener() {
// TODO Auto-generated constructor stub
}

/**
     *@see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("Context destroyed -"+arg0.getSource()); }

/**
     *@see ServletContextListener#contextInitialized(ServletContextEvent)
     */
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Context Initialized --"+arg0.getSource()); }

}
 Request Listener
package com.vinod.test;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;

/**
 * Application Lifecycle Listener implementation class RequestListener
 *
 */
/**
 *@authorvinodkariyathungalkumaran
 *
 */
@WebListener
public class RequestListener implements ServletRequestListener {

/**
     * Default constructor.
     */
public RequestListener() {
// TODO Auto-generated constructor stub
}

/**
     *@see ServletRequestListener#requestDestroyed(ServletRequestEvent)
     */
public void requestDestroyed(ServletRequestEvent arg0) {
System.out.println("Request destroyed "+arg0.getServletRequest().getRemoteAddr()); }

/**
     *@see ServletRequestListener#requestInitialized(ServletRequestEvent)
     */
public void requestInitialized(ServletRequestEvent arg0) {
System.out.println("Request Initialized from "+arg0.getServletRequest().getRemoteAddr()); }

}
 
 
If you are not using the annotation add listener in web.xml, example
 
<listener> 
<listener-class>com.vinod.test.MySessionListener</listener-class> 
</listener>
 
Create a servlet and create session over there for the requests.

package com.vinod.test;

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

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ListenerTestServlet
 */
@WebServlet("/ListenerTestServlet")
public class ListenerTestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
    *@see HttpServlet#HttpServlet()
    */
public ListenerTestServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
    *@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<HTML><TITLE>HttpSession Example</TITLE><BODY>");

// Creating a session
HttpSession session = request.getSession(true);
String value = (String) session.getAttribute("UserName");

if (value == null) {
String sessionObject = "prsession";
session.setAttribute("UserName", sessionObject);
}

out.println("Your Session created " + session.getId());
out.println("</BODY><HTML>");
out.close();
}

/**
    *@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}

Output
Deploy and run the servlet and in the console we can see the session id has been printed which we logged in the listener class.
 
Context Initialized --org.apache.catalina.core.ApplicationContextFacade@764c9394
Request Initialized from 0:0:0:0:0:0:0:1
Request destroyed 0:0:0:0:0:0:0:1
Request Initialized from 0:0:0:0:0:0:0:1
Session created 036D66CAF28A7B2731736F40D01C00D2
Request destroyed 0:0:0:0:0:0:0:1

 

 

0 comments: