我正在使用JEE 6项目,客户有时需要防止会话超时。我想使用布尔复选框来允许用户保持联系或不想要他人。
我受到以下技术的诱惑,其中 myType 必须是:客户端或服务器;
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>#{mySession.myType}</param-value>
</context-param>
答案 0 :(得分:1)
通过部署描述符,将其设置为-1
将使其无限期:
<session-config>
<session-timeout>
-1
</session-timeout>
</session-config>
答案 1 :(得分:1)
我以其他方式解决了这个问题:
评论:
<强>的web.xml 强>
<session-config\>
<session-timeout>20</session-timeout>
</session-config>
<强>登录表单强>
<form method=post action="/j_security_check.jsp" >
<input type="text" name= "j_username" >
<input type="password" name= "j_password" >
<input type="checkbox" name="j_remember" />
</form>
<强> j_security_check.jsp 强>
//Have we already authenticated someone ?
if (request.getUserPrincipal() == null) {
String j_username = request.getParameter("j_username");
String j_password = request.getParameter("j_password");
String j_remember = request.getParameter("j_remember");
try {
request.login(j_username, j_password);
if("on".equals(j_remember)){
session.setAttribute(KEEP_CONNECT, true);
} else {
session.setAttribute(KEEP_CONNECT, false);
}
logger.debug("Authentication of '" + request.getUserPrincipal() + "' was successful.");
response.sendRedirect(request.getContextPath() +HOME_PAGE);
} catch (Exception ex) {
logger.error(ex,"Authentication failed.");
response.sendRedirect(request.getContextPath() + ERROR_PAGE);
}
} else {
logger.debug("Already authenticated '" + request.getUserPrincipal() + "'.");
response.sendRedirect(request.getContextPath() + LOGIN_PAGE);
}
<强> SessionManagedBean 强>
private void initTimeOut() {
String login = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName();
boolean keepConnected = (boolean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(KEEP_CONNECT);
logger.debug(login + " IN > " + userSession.getMaxInactiveInterval());
logger.debug(" keepConnected ? = " + keepConnected);
if (keepConnected) {
//keep this session and disable timeOut until the manual deconnexion
userSession.setMaxInactiveInterval(-1);
}
logger.debug(login + " OUT > " + userSession.getMaxInactiveInterval());
}