我已經解決了其他方式這個問題:
- 我沒有使用javax.faces.STATE_SAVING_METHOD
- 在我的web.xml我用:會話超時= 20
- 在我登錄表單
- 我從j_security_check通過創建改變動作形式要j_security_check.jsp JSP文件 。
- 我已經在登錄表單一個複選框加入到知道,如果用戶想留連接與否。
- 以我managedBean我檢查KEEP_CONNECT值,禁用超時,直到手動deconnexion:userSession.setMaxInactiveInterval(-1);或保持此會話更長時間(2小時):userSession.setMaxInactiveInterval(7200);
審查:
的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());
}
你好這個話題已經在這裏討論:[http://stackoverflow.com/questions/2960764/how-to-set-session-timeout-dynamically - 在Java的Web應用程序](http://stackoverflow.com/questions/2960764/how-to-set-session-timeout-dynamically-in-java-web-applications)在這裏:[http:// stackoverflow.com/questions/15382895/session-timeout-in-web-xml](http://stackoverflow.com/questions/15382895/session-timeout-in-web-xml) – aelkz