Solved

Accessing HttpSession object from jspDestroy()

Posted on 2001-08-09
3
224 Views
Last Modified: 2010-03-31
Is it possible to access the session object in the jspDestroy() method from a JSP page?

I am trying to catch the session timeout to store current preferences in the database.

0
Comment
Question by:trobison
  • 2
3 Comments
 

Accepted Solution

by:
harshita earned 50 total points
ID: 6374534
There is always an event associated with the HttpSession.
It is HttpSessionBindngEvent. IF you want to track whenever the session is closed, you need to write a wrapper class which needs  to implement HttpSessionBindingListener. In this class you need to implement 2 methods provided by the interface namely valueBound and valueUnbound. These 2 methods take the HttpSessionBindingEvent as a parameter. Whenever your session expires or timedout this valueUnbound method fires automatically. You need to decide what to do when the session timed out.
So your syntax would be like this. You need to pass the conenction object to this class constructor.

public class sessiontest implements HttpSessionBindingListener
{
    java.sql.Connection con = null;
    public sessiontest(Connection con1)
    {
          con = con1;
    }

    //Here please checkout the syntax required for the method declaration of value bound and valueunbound. Idin't remember exactly..but it would be like this

     valueBound(HttpSessionBindingEvent evt)
     {
       //do some processing
     }

    valueUnBound(HttpSessionBindingEvent evt)
    {
    //AS this method automatically fired when the session gets timedout, you dcan directly check whether the database connection still exists or not.If it still exists, then do whatever you want. In this case I have rolled back the teransaction.
         try
         {
             if (con != null)
                    con.rollback();
         }
         catch(SQLException e)
         {
         }
    }
}


Once you developed the above listener class, you need o use it in the follwoing way in your regular code.

.....
....some connection creation code goes....
.....
con = DriverManager.getConnection(.......);
//create the instance of your sessiontime out class
sessiontest st = new sessiontest(con);

//here onwards you can do your other regular coding stuff.
This means you have registered your connection object with the HttpSession. So that you iwll comne to know whenevedr your session times out, you wil ldo some balanace database trnasactions before totally loosing the session data.

Hope this explains your answer. So you need not go for jspDestroy method. correct me if i am wrong.
0
 

Author Comment

by:trobison
ID: 6374593
Yes harshita, you are correct. That is the alternative I have used, and it works pretty well for my solution.

However, I have other developers in my group that would like to access the session object in the init and destroy methods.

I think you can only get to the ServletContext level but you need the request to get the HttpSession object. I can get this far, but this only allows access to attributes bound to the ServletContext. Still cannot figure out how to get the session object from here?

      ServletConfig cfg = getServletConfig();
      ServletContext ctx = cfg.getServletContext();

Any ideas?
0
 

Author Comment

by:trobison
ID: 6385941
Thanks for your input....

Terry
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
what is a "java.lang.System Property"   ? 20 57
couple of eclipse 5 25
factorial example 4 35
javap not working 8 39
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now