HttpSessionAttributeListener Example

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:
- attributeAdded(HttpSessionBindingEvent event):
It notifies whenever a new attribute is added to the servlet session.
- attributeRemoved(HttpSessionBindingEvent event):
It notifies whenever the attribute is removed from the servlet session.
- attributeReplaced(HttpSessionBindingEvent event):
It notifies whenever the attribute gets replaced on the servlet session.
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:
- getName() : This method returns the name of
the attribute that has been change in the session.
- getValue(): This method will return the value
of the attribute that has been added, removed or replaced by other
attribute.
- getSession(): This method will return the
session that has been changed.
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:

Download
this example.

|