Hi all,

Somebody was asking about how to capture the sessionbindingevent and get
notified.

Here is the sample code for that. HTH

I am using jsdk2.1 so commented setAttribute()
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SessionItemNotification extends HttpServlet
{
  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    HttpSession session = req.getSession();

   // session.setAttribute("cone", new KnowAllItem("snake eyes") );
    session.putValue("cone",new KnowAllItem("snake Eyes"));
   // session.removeAttribute("cone");
      session.removeValue("cone");

    res.getWriter().println( "check your console..." );
  }
}

/**
 * By implementing HttpSessionBindingListener, KnowAllItem objects get
 * informed when they are added and/or removed from a servlet session
 *
 * Also, remember to make you session items serializable
 */
class KnowAllItem implements HttpSessionBindingListener, Serializable
{
  private String id;

  public KnowAllItem(String theId)
  {
    id = theId;
  }

  public void valueBound(HttpSessionBindingEvent evt)
  {
    System.out.println( "added KnowAllItem to session -> " + dump(evt) );
  }

  public void valueUnbound(HttpSessionBindingEvent evt)
  {
    System.out.println( "removed KnowAllItem from session -> " + dump(evt)
);
  }

  private String dump(HttpSessionBindingEvent evt)
  {
    return "\n attribute name: " + evt.getName() + "\n session: " +
evt.getSession() +
//"\n attribute value :"+ evt.getValue() ;
  }

  public String toString()
  {
    return id;
  }
}
This e-mail and any files transmitted with it are for the sole use of the intended 
recipient(s) and may contain confidential and privileged information.
If you are not the intended recipient, please contact the sender by reply e-mail and 
destroy all copies of the original message.
Any unauthorised review, use, disclosure, dissemination, forwarding, printing or 
copying of this email or any action taken in reliance on this e-mail is strictly
prohibited and may be unlawful.

                Visit us at http://www.cognizant.com

Reply via email to