Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

newbie question : HttpSessionBindingListener Interface

Thread view: 
pincopallo_it@yahoo.it - 06 Feb 2006 07:24 GMT
I got a servlet and and I d like to use the interface
HttpSessionBindingListener  to check when session expires and give a
message to the user
I did not find examples about how to use it...
Anyone can help ?
thats what I do:
1) public class GUIControllerRead extends HttpServlet
   public class GUIControllerRead extends HttpServlet implements
HttpSessionBindingListener  // I add the interface in this way
2)I added this code
HttpSessionBindingEvent firstry= new HttpSessionBindingEvent;

   public void valueUnbound(firstry) {
    System.out.println ("Expired session " );
   }
   public void valueBound(firstry) {
    System.out.println ("Active session " );
   }

protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
        throws ServletException
 {

                int id_eins;

                session = request.getSession();
               request.setAttribute("guicontroller",
this.getClass().getName());

                Enumeration en = request.getParameterNames();
                while (en.hasMoreElements()) {
            ...

Thanks
Gianni
stefanomnn - 06 Feb 2006 12:42 GMT
Hi, Gianni, it's simple! you make a class like this

package mypack;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;

public class MySessionListener implements
javax.servlet.http.HttpSessionListener,
javax.servlet.http.HttpSessionAttributeListener
{
 private String name = null;
 private HttpSession session = null;

 public void sessionCreated(HttpSessionEvent event)
 {
   session = event.getSession();
 }

 public void sessionDestroyed(HttpSessionEvent event)
 {
   session = event.getSession();
  /* handle event */
 }

 public void attributeAdded(HttpSessionBindingEvent event)
 {
   name = event.getName();
   session = event.getSession();
 }

 public void attributeRemoved(HttpSessionBindingEvent event)
 {
   name = event.getName();
   session = event.getSession();
 }

 public void attributeReplaced(HttpSessionBindingEvent event)
 {
   name = event.getName();
   session = event.getSession();
 }
}

then, in web.xml, add this (before declaring your servlet):

 <listener>
   <listener-class>mypack.MySessionListener</listener-class>
 </listener>

i hope i helped you!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.