0

I need to change an attribute in ServletContext when a User Session is expired.

How suggest here how-to-call-a-method-before-the-session-object-is-destroyed, I've implemented the method valueUnbound(HttpSessionBindingEvent event) in my java class, who allow to access to Session object before it's destroyed using event reference.

Inside this method I need to change a value in array that is attribute in ServletContext. How can I do?

public class myClass implements HttpSessionBindingListener {

@Override
public void valueUnbound(HttpSessionBindingEvent event) {
    int userid = Integer.valueOf((Integer) event.getSession().getAttribute("IDplayer"));
    boolean[] id_used = (boolean[]) getServletContext().getAttribute("id_used"); 
}

The problem is that getServletContext().getAttribute() is not founded, also if I included the "import javax.servlet.*".

How can I access to ServletContext attribute from a method called before the session is closed?

Community
  • 1
  • 1
Gabrer
  • 465
  • 5
  • 21

2 Answers2

1

Access it via HttpSessionBindingEvent. Use this:

 event.getSession().getServletContext().getAttribute("id_used"); 
Manish Maheshwari
  • 4,045
  • 2
  • 15
  • 25
1

Try this code

session = event.getSession();  
contextObj = session.getServletContext();
Saurabh Arora
  • 80
  • 1
  • 9