This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
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

wood burning stoves
subject: HttpSessionListener
It's not a secret anymore!