In dynamic web application user may like to track as and when object gets "bound" and "unbound" from the session object. In Java EE it is possible to achieve through "Listeners".
HttpSessionBindingListener has below two methods:
1) public void valueUnbound(HttpSessionBindingEvent arg0)
2) public void valueBound(HttpSessionBindingEvent arg0)
If we want to perform any action while object is associated or removed from the "HttpSession" then below three steps needs to be performed:
1) Modify web.xml (deployment descriptor) file to add listener
2) Add a class which implements "HttpSessionBindingListener"
3) Run and Verify
Lets see the steps in detailed:
1) Modify web.xml (deployment descriptor) file to add listener
web.xml file will be read / looked upon by the container while loading any of the project. To inform container about "HttpSessionBindingListener" we need to add below
lines in it:
<listener>
<listener-class>com.listener.Student</listener-class>
</listener>
2) Add a class which implements "HttpSessionBindingListener"
Here is the code snippet for the quick reference:
public class Student implements HttpSessionBindingListener {
public Student() {
}
public void valueUnbound(HttpSessionBindingEvent arg0) {
System.out.println("Student Object is Unbound");
}
public void valueBound(HttpSessionBindingEvent arg0) {
System.out.println("Student Object is Bound");
}
}
As we can see there are two methods "valueUnbound" and "valueBound" which will be called at the time of "Student" object gets added or removed to and from the HttpSession object respectively.
3) Run and Verify
Lets create a dummy servlet called "HttpSessionBindEventTest" which has below "doGet" method which adds and removes the "Student" object in HttpSession.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student student = new Student();
request.getSession().setAttribute("student", student); //Line #1
request.getSession().removeAttribute("student"); //Line #2
}
when we run the Url : "http://localhost:8080/student/HttpSessionBindEventTest" and hit enter below lines we can see inside logs:
INFO: Server startup in 578 ms
Student Object is Bound //Line #1
Student Object is Unbound //Line #2
Line #1 is the result of 1st line of the "doGet" method (HttpSessionBindEventTest)
Line #2 is the result of the 2nd line of the "doGet" method (HttpSessionBindEventTest)
Cheers!
Henal Saraiya
(Lead Consultant)
CIGNEX Datamatics
HttpSessionBindingListener has below two methods:
1) public void valueUnbound(HttpSessionBindingEvent arg0)
2) public void valueBound(HttpSessionBindingEvent arg0)
If we want to perform any action while object is associated or removed from the "HttpSession" then below three steps needs to be performed:
1) Modify web.xml (deployment descriptor) file to add listener
2) Add a class which implements "HttpSessionBindingListener"
3) Run and Verify
Lets see the steps in detailed:
1) Modify web.xml (deployment descriptor) file to add listener
web.xml file will be read / looked upon by the container while loading any of the project. To inform container about "HttpSessionBindingListener" we need to add below
lines in it:
<listener>
<listener-class>com.listener.Student</listener-class>
</listener>
2) Add a class which implements "HttpSessionBindingListener"
Here is the code snippet for the quick reference:
public class Student implements HttpSessionBindingListener {
public Student() {
}
public void valueUnbound(HttpSessionBindingEvent arg0) {
System.out.println("Student Object is Unbound");
}
public void valueBound(HttpSessionBindingEvent arg0) {
System.out.println("Student Object is Bound");
}
}
As we can see there are two methods "valueUnbound" and "valueBound" which will be called at the time of "Student" object gets added or removed to and from the HttpSession object respectively.
3) Run and Verify
Lets create a dummy servlet called "HttpSessionBindEventTest" which has below "doGet" method which adds and removes the "Student" object in HttpSession.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student student = new Student();
request.getSession().setAttribute("student", student); //Line #1
request.getSession().removeAttribute("student"); //Line #2
}
when we run the Url : "http://localhost:8080/student/HttpSessionBindEventTest" and hit enter below lines we can see inside logs:
INFO: Server startup in 578 ms
Student Object is Bound //Line #1
Student Object is Unbound //Line #2
Line #1 is the result of 1st line of the "doGet" method (HttpSessionBindEventTest)
Line #2 is the result of the 2nd line of the "doGet" method (HttpSessionBindEventTest)
Cheers!
Henal Saraiya
(Lead Consultant)
CIGNEX Datamatics
No comments:
Post a Comment