HttpSessionBindingEvent example


 

HttpSessionBindingEvent example

In this tutorial you will see how HttpSessionBindingEvent is used in the servlet.

In this tutorial you will see how HttpSessionBindingEvent is used in the servlet.

Description:

The class HttpSessionBindingEvent exists in the javax.servlet.http package. Its event are sent to an object that implements the HttpSessionListener when it need to bound or unbound a session.

Code:

package roseindia.servletexample;

import javax.servlet.http.*;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;

public class HttpSessionBindingEventExample extends HttpServlet {

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

PrintWriter out = response.getWriter();

HttpSession session = request.getSession(true);
HttpSessionBindingEvent bind = null;
bind = new HttpSessionBindingEvent(session, "sessionbinding");

if (bind != null) {
out.println("sesion binding sucessfully ");

} else {
out.println("sesion binding failed");

}
}
}

Output:

The result is conditional one when the variable bind is holding the name of the object that has been bound to or unbound from a session. If the variable hold some some value it will display "session binding sucessfully" else "sesion binding failed".

Ads