Solved

Accessing HttpSession object from jspDestroy()

Posted on 2001-08-09
3
249 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example issue 2 40
how to debug htl and js pages 8 77
java mysql insert application 14 66
How to log java errors in tomcat 8 35
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

737 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