Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

HttpSessionAttributeListener example using @WebListener annotation.

Program to demonstrate how to apply listener to Session attributes in Java EE

package com.hubberspot.javaee.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

// @WebListener annotation informs container that
// this class is a web listener which will listen to
// various events happening during lifecycle of application
// Here this class listens to changes made to HttpSession 
// attributes.

// We make class implements HttpSessionAttributeListener
// which has three methods : attributeRemoved(), attributeAdded() 
// and attributeReplaced() , which are called by the container
// whenever attributes are added, removed, or replaced 
// within the HTTP session.

@WebListener
public class MySessionAttributeListener implements HttpSessionAttributeListener {


    public void attributeRemoved(HttpSessionBindingEvent event) {

     System.out.println("Method called when HttpSession attribute removed :");
     HttpSession session = event.getSession();     
     System.out.println("Session ID : " + session.getId());
     System.out.println("Session Name : " + event.getName());
     System.out.println("Session Value : " + event.getValue());
     
    }

    public void attributeAdded(HttpSessionBindingEvent event) {

     System.out.println("Method called when HttpSession attribute added :");
     HttpSession session = event.getSession();     
     System.out.println("Session ID : " + session.getId());
     System.out.println("Session Name : " + event.getName());
     System.out.println("Session Value : " + event.getValue());
     
    }

    public void attributeReplaced(HttpSessionBindingEvent event) {

     System.out.println("Method called when HttpSession attribute replaced :");
     HttpSession session = event.getSession();     
     System.out.println("Session ID : " + session.getId());
     System.out.println("Session Name : " + event.getName());
     System.out.println("Session Value : " + event.getValue());
     
    }
 
}


Run HelloWorldServlet.java and methods mentioned in above listener will be called by the container.


package com.hubberspot.javaee;

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

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
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;

// @WebServlet annotation has a initParams field which takes
// in initialization parameters for a servlet.
// @WebInitParam annotation takes in a name and value for the
// initialization parameters for the current Servlet.

@WebServlet(name = "HelloWorldServlet" , urlPatterns = { "/HelloWorldServlet" }
    , initParams = { @WebInitParam(name = "user" , value = "Jonty") })
public class HelloWorldServlet extends HttpServlet {

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

  response.setContentType("text/html");

  PrintWriter out = response.getWriter();
  
  HttpSession session = request.getSession();
  
  // attributeAdded method gets executed
  session.setAttribute("user", "Jonty");
  
  // attributeReplaced method gets executed
  session.setAttribute("user", "Dinesh");
  
  // attributeRemoved method gets executed
                session.removeAttribute("user");

 }

}


Output of the program : 


 
 
© 2021 Learn Java by Examples Template by Hubberspot