File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes HttpSessionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpSessionListener" Watch "HttpSessionListener" New topic


Chandra Sekhar
Ranch Hand

Joined: Sep 26, 2003
Posts: 90

Can someone help me in understanding the usage of HttpSessionListener Interface.

Thanks in Advance
Chandrasekhar S.

"Luck is when opportunity meets preparation"
Atul Prabhu
Ranch Hand

Joined: Dec 17, 2002
Posts: 60
package servlets;
import javax.servlet.*;
import javax.servlet.http.*;

public class SessionServlet extends HttpServlet implements HttpSessionListener

public void doGet(HttpServletRequest request,HttpServletResponse response)
HttpSession session = request.getSession();
catch(Exception e){
System.out.println("Exception: " + e);

public void sessionCreated(HttpSessionEvent se)
System.out.println("Session Created ");

public void sessionDestroyed(HttpSessionEvent se)
System.out.println("Session Destroyed ");



If u see above SessionServlet has implemented HttpSessionListener. So when a session is created , method sessionCreated() is called. Similarly sessionDestroyed is called when session is destroyed by the container.

[ September 16, 2004: Message edited by: Atul Prabhu ]
James Clinton
Ranch Hand

Joined: Jun 23, 2003
Posts: 190
Useful for clearing up database connections. HttpSessionListner and others (Context) are clearly explained in the following book:

SCWCD Exam Study Kit, Java Web Component Developer Certifcation
Deshmukh and Malavia

I agree. Here's the link:
subject: HttpSessionListener
jQuery in Action, 3rd edition