【问题标题】:Why are the values of my application retained even after server restart?为什么我的应用程序的值即使在服务器重新启动后仍然保留?
【发布时间】:2013-10-07 02:41:43
【问题描述】:

我的小应用程序中有 2 个@SessionScoped bean。即使我重新启动清理服务器,所有表单值都会保留。

@ManagedBean(name = "bill")
@SessionScoped
public class Bill implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final String MANAGED_BEAN_NAME = "bill";

/**
     * @return the current instance of Bill
     */
    public static Bill getCurrentInstance() {
        return (Bill) FacesContext.getCurrentInstance().getExternalContext()
                .getSessionMap().get(MANAGED_BEAN_NAME);
    }
//other methods
}

@ManagedBean
@SessionScoped
public class TailorTip implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private Bill bill;
private Participants[] participants;
private Integer participantIndex;

@PostConstruct
public void init() {
    bill = Bill.getCurrentInstance();
}

//other methods
}

如何避免保留值?注意:我用的是tomcat。

【问题讨论】:

    标签: jsf tomcat


    【解决方案1】:

    这确实在意料之中。许多服务器在重启时保存和恢复会话,以便最终用户可以继续他们的网站会话。这是通过 Java 序列化实现的。这也正是会话作用域 bean 需要实现 Serializable 的原因。

    至于解决方案,有几种方法可以解决这个问题:

    1. 将 bean 置于作业的正确范围内。您绝对确定它们需要在会话范围内吗?毕竟,视图范围不是更适合那些 bean 吗?会话范围通常仅用于特定于客户端的信息,例如登录用户、其偏好等,因此不适用于表单数据。您显然迫切需要在每次服务器重新启动时清除它们,这证实了会话范围是错误的选择。另见How to choose the right bean scope?

    2. 去掉类中的Serializable接口声明。这样他们将无法在服务器重新启动时恢复。

    3. 告诉 Tomcat 在服务器重启时不要恢复会话。编辑 context.xml 以将带有空 pathname<Manager> 元素添加到 <Context> 元素。

      <Context ... >
          <Manager pathname="" />
      </Context>
      

    另见:

    【讨论】:

      【解决方案2】:

      如果它们是会话范围的,那么许多服务器会在重新启动后保留会话信息。如果您重新部署应用程序,则信息将被清除。但简单地重新启动应用程序不会重置它。您必须查阅服务器文档才能在服务器重启时禁用会话持久性。

      【讨论】:

      • 我使用 Tomcat。知道我可以在哪里做你刚才说的吗?
      猜你喜欢
      • 1970-01-01
      • 2018-04-21
      • 2012-05-27
      • 1970-01-01
      • 2019-12-05
      • 2017-04-12
      • 1970-01-01
      • 2023-03-11
      • 2013-10-03
      相关资源
      最近更新 更多