As we know that the Session is used to maintain the session between request. Session object is responsible to hold the conversational state across multiple requests.
The listener HttpSessionAttributeListener is an interface and extends the java.util.EventListener class. This listener will be called by the container whenever there there will be change to the attribute list on the servlet session of a web application. This listener is used when we want to know when a attribute has been added in a session, when a attribute has been removed and when it is replaced by another attribute. We can also say that this attribute is used when the developer is interested to be notified when the session attribute changes. Now you may be wondering what is an attribute. An attribute is an object set or you can simply say that it is name/value pair where the name refers to a String and a value refers to the Object.
javax.servlet.http.HttpSessionAttributeListener interface has following methods:
In the above methods you can see that we have used HttpSessionBindingEvent class as a parameter to the above methods. This class is a event class which is used for notifications when the changes are made to the attributes of in a session.
The class HttpSessionBindingEvent has two methods:
The code of the program is given below:
| import javax.servlet.*; import javax.servlet.http.*; public class SessionAttributeListenerExample implements HttpSessionAttributeListener { public void attributeAdded(HttpSessionBindingEvent sessionBindingEvent) { // Get the session HttpSession session = sessionBindingEvent.getSession(); // Log some information System.out.println("[SessionAttr] "+new java.util.Date()+ " Attribute added, session "+session+": " +sessionBindingEvent.getName()+"="+ sessionBindingEvent.getValue()); } public void attributeRemoved(HttpSessionBindingEvent sessionBindingEvent) { // Get the session HttpSession session = sessionBindingEvent.getSession(); System.out.println(new java.util.Date()+" Attribute removed, session "+session+": "+sessionBindingEvent.getName()); } public void attributeReplaced(HttpSessionBindingEvent sessionBindingEvent) { // Get the session HttpSession session = sessionBindingEvent.getSession(); // Log some information System.out.println(new java.util.Date()+" Attribute replaced, session "+session+": "+sessionBindingEvent .getName()+"="+sessionBindingEvent.getValue()); } } |
| import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class AttributeSessionForSession extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); HttpSession session = request.getSession(); session.setAttribute("dog", "Labrador"); session.setAttribute("name", "moti"); session.setAttribute("age","5"); String str1 = (String)session.getAttribute("dog"); pw.println("The breed of the dog is " + str1); String str2 = (String)session.getAttribute("age"); pw.println("The age of the dog is " + str2); session.removeAttribute("name"); } } |
The output of the program is given below:

The output on the server will look like this:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: HttpSessionAttributeListener Example View All Comments
Post your Comment