?
Solved

Accessing HttpSession object from jspDestroy()

Posted on 2001-08-09
3
Medium Priority
?
258 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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

800 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