从bean超时 [英] Timing out from a bean

查看:65
本文介绍了从bean超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望会话在给定的时间间隔后超时.在web.xml中,我一直在使用如下代码:

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>   

其中20是超时时间(以分钟为单位),可以正常工作.

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

我想做的是在我的一个bean中使用如下代码以编程方式进行此操作:

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   

}  

,此处的超时时间为1200秒,即20分钟.不幸的是,在打开浏览器查看该应用程序时,它失败并显示以下消息:

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. 

其次:

java.lang.NullPointerException 

我在这里做错了什么?我知道setMaxInactiveInterval()是指特定的会话,在这种情况下是登录Bean,而不是所有内容,而这正是web.xml文件中的代码所指定的.我有几个bean,但是使登录bean超时是唯一重要的.

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.

我正在将JSF 2.0与Glassfish 3.1.1和Eclipse Indigo一起使用,因此非常感谢您提供一些建议.

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

推荐答案

NullPointerException有一个非常简单的原因.这是最简单的例外之一.要了解任意异常的原因,只需查看其javadoc.所有Java异常都有其原因在javadoc中进行了解释.这是NullPointerException javadoc的摘录:

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:

在需要对象的情况下应用程序尝试使用null时抛出.这些包括:

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

  • 调用null对象的实例方法.
  • 访问或修改null对象的字段.
  • null的长度作为数组.
  • 访问或修改null的插槽,就好像它是一个数组一样.
  • 抛出null就像是一个Throwable值.
  • 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.

应用程序应抛出此类的实例,以指示对null对象的其他非法使用.

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

您的问题是由点1引起的.在这里,

Your problem is caused by point 1. Here,

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

您正在尝试在null而不是具体的HttpServletRequest实例上调用getSession()方法.实际上,您应该已经通过ExternalContext#getRequest()获得了HttpServletRequest并将其分配给request.

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.

但是,您遇到了更大的问题:您绝对不应该将当前的servlet请求作为会话作用域的bean(其寿命比HTTP请求更长!)的属性来控制.您应该在线程局部范围内获取它(即完全在构造函数或方法块内部).您也不应让您的JSF托管Bean实现HttpSessionListener.这完全没有道理.您将最终得到2个实例,一个实例由容器创建为侦听器,另一个实例由JSF创建为托管bean.

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.

因此应该这样做:

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

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

    // ...
}

或者,如果您使用的是JSF 2.1,请使用

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

FacesContext.getCurrentInstance().getExternalContext().setSessionMaxInactiveInterval(1200);

这篇关于从bean超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆