//ListenerSession.java import javax.servlet.*; import javax.servlet.http.*; 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 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); } }