Breaking News
Home / ServerSide / Session Listener Filter

Session Listener Filter

A simple session listener can help to understand the beginning and closing of Http session. All we have to do is to implement the HttpSessionListner interface. The interface provides basic methods like sessionCreated and sessionDestroyed. In the implementation of these method we can add logic to capture and log the details of session.

Snippet

  1. package wlsunleashed.servlets;
  2.  
  3. import javax.servlet.http.HttpSession;
  4. import javax.servlet.http.HttpSessionEvent;
  5. import javax.servlet.http.HttpSessionListener;
  6.  
  7. /*
  8. *  Simple Session Listener for maintaining the user’s footprints
  9. */
  10.  
  11. public class SimpleSessionListener implements HttpSessionListener {
  12.  
  13.         HttpSession session = null;
  14.  
  15.         // default constructor
  16.         public SimpleSessionListener() {
  17.         }
  18.  
  19.         // Life cycle event invoked when the session is created
  20.         // Adds a counter to the session to tract the user’s visit to the site
  21.         public void sessionCreated(HttpSessionEvent evt) {
  22.                 session = evt.getSession();
  23.                 Long visitCount = new Long(1);
  24.                 System.out.println("Session Created");
  25.                 session.setAttribute("visit_Count", visitCount);
  26.         }
  27.  
  28.         // Life cycle event invoked when the session is destroyed
  29.         // Prints the total number of visits to the server by the server
  30.         public void sessionDestroyed(HttpSessionEvent evt) {
  31.                 session = evt.getSession();
  32.                 System.out.println("Session Destroyed");
  33.                 Long visitCount = (Long) session.getAttribute("visit_Count");
  34.                 if (visitCount == null)
  35.                         visitCount = new Long(1);
  36.                 else
  37.                         visitCount = new Long(visitCount.longValue() + 1);
  38.                 System.out.println("Total Number of Hits=" + visitCount.longValue());
  39.  
  40.         }
  41. }
[pb_builder]

Check Also

What webservices standards are supported by Siebel Business Applications?

The following Web services standards are supported by Siebel Business Applications:     * Web Services …

Advertisment ad adsense adlogger