Solved

Accessing HttpSession object from jspDestroy()

Posted on 2001-08-09
3
214 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your input....

Terry
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

771 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

10 Experts available now in Live!

Get 1:1 Help Now