This section contains detail about Session Listener implementation in java.
Session Listener Example
Listener is predefined interface available in life cycle of application. Session Listener deals with the session object. For HTTP session events, the event listener classes can receive notification when an HTTP session is activated or is about to be deactivated, and when an HTTP session attribute is added, removed, or replaced.
Example
ListenerSession.java(Listener Class)
package j2ee; import javax.servlet.http.*; import javax.servlet.http.HttpSessionListener; public class ListenerSession implements HttpSessionListener { public ListenerSession() { } public void sessionCreated(HttpSessionEvent sessionEvent) { // Get the session that was created HttpSession session = sessionEvent.getSession(); // Store something in the session, and log a message try { System.out.println("Session created: " + session); session.setAttribute("dog", "labrador"); session.setAttribute("name", "Diana"); } catch (Exception e) { System.out.println("Error in setting session attribute: " + e.getMessage()); } } public void sessionDestroyed(HttpSessionEvent sessionEvent) { // Get the session that was invalidated HttpSession session = sessionEvent.getSession(); // Log a message System.out.println("Session invalidated: " + session); System.out.println("The breed of the dog is: " + session.getAttribute("dog")); System.out.println("The name of the dog is : " + session.getAttribute("name")); } }
ServletListenerSession.java(Servlet)
package j2ee; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class ServletListenerSession extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); HttpSession session = request.getSession(); String str = (String) session.getAttribute("dog"); String dogName = (String) session.getAttribute("name"); pw.println("The breed of the dog is " + str); pw.println("The name of the dog is " + dogName); session.invalidate(); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>J2EE</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>ServletListenerSession</display-name> <servlet-name>ServletListenerSession</servlet-name> <servlet-class>j2ee.ServletListenerSession</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletListenerSession</servlet-name> <url-pattern>/ServletListenerSession</url-pattern> </servlet-mapping> <listener> <listener-class>j2ee.ListenerSession</listener-class> </listener> </web-app>
Output
When you execute the following code, you will get the following output :
In console you will get the following message :
[ 0 ] Comments