Solved

Accessing HttpSession object from jspDestroy()

Posted on 2001-08-09
3
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

695 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