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"));
	}
}
