1

I want my session to timeout after a given interval of time. In web.xml I've been using code like:

<session-config>   
  <session-timeout>20</session-timeout>   
</session-config>   

where 20 is the timeout period in minutes, which works correctly.

What I would like to do is to do it programatically using code like this inside one of my beans as follow:

@ManagedBean(name="login")   
@SessionScoped  
public class MyLoginBean implements HttpSessionListener, Serializable {   

    // private variables etc.   

    HttpServletRequest request;   
    HttpSession session  = request.getSession();   

    // Constructor
    public MyLoginBean() {   
        session.setMaxInactiveInterval(1200);   
    }   

// The rest of the code   

}  

where the timeout here is 1200 seconds, i.e. 20 minutes. Unfortunately, on opening up a browser to look at the application, it fails with the message:

com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.csharp.MyLoginBean. 

Followed by:

java.lang.NullPointerException 

What am I doing wrong here? I know that setMaxInactiveInterval() refers to the particular session, which in this case is the login bean, rather than everything, which is what the code in web.xml file specifies. I have several beans, but timing out the login bean is the only one that matters.

I'm using JSF 2.0 with Glassfish 3.1.1 and Eclipse Indigo, so some advice would be very much appreciated.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
csharp
  • 464
  • 2
  • 10
  • 19

1 Answers1

4

The NullPointerException has an extremely simple cause. It's one of the most simplest exceptions. To learn about the cause of an arbitrary exception, just look in its javadoc. All Java exceptions have their causes explained in the javadoc. Here's an extract of the javadoc of NullPointerException:

Thrown when an application attempts to use null in a case where an object is required. These include:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

Applications should throw instances of this class to indicate other illegal uses of the null object.

Your problem is caused by point 1. Here,

HttpServletRequest request;   
HttpSession session  = request.getSession();   

you're trying to invoke getSession() method on null instead of a concrete HttpServletRequest instance. In fact, you should have obtained the HttpServletRequest via ExternalContext#getRequest() and assigned it to request.

However, you've bigger problems: you should absolutely not get hold of the current servlet request as a property of a session scoped bean (which lives longer than the HTTP request!). You should get it inside the thread local scope (i.e. wholly inside the constructor or the method block). You should also not let your JSF managed bean implement the HttpSessionListener. This makes no utter sense. You'd end up with 2 instances, one created as listener by the container and another one created as managed bean by JSF.

Just thus should do:

@ManagedBean(name="login")
@SessionScoped
public class MyLoginBean implements Serializable {

    public MyLoginBean() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession();
        session.setMaxInactiveInterval(1200);
    }

    // ...
}

Or, if you're using JSF 2.1, use the one provided by ExternalContext:

FacesContext.getCurrentInstance().getExternalContext().setSessionMaxInactiveInterval(1200);
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Many thanks indeed, particularly as I din't know much about this. Eclipse complained that getSession() didn't have a boolean argument, so I made it true and it works by testing it out with a very short timeout. When the session expires, how do I let the user know, either directing to another page, or putting a message up on the page displayed? Thanks for any ideas. – csharp Jan 29 '13 at 12:52
  • You're welcome. As to the new question, that's a different question. Just do search (I've surely answered this several times before) or ask a new question. – BalusC Jan 29 '13 at 12:53
  • Can I set the timeout inside of the annotation? – ptkato Nov 15 '14 at 03:44