如何在servlet中设置会话超时

时间:2016-08-19 12:40:52

标签: servlets

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

    if (session == null) {
        String forwardTo = "/jsp/login.jsp?status=session";
        ServletContext servletContext = filterConfig.getServletContext();
        javax.servlet.RequestDispatcher dispatcher = servletContext
                .getRequestDispatcher(forwardTo);
        if (dispatcher != null) {
            try {
                dispatcher.forward(request, response);
            } catch (Exception ex) {
                ex.printStackTrace();
            } catch (Throwable ex) {
                ex.printStackTrace();
            }

的web.xml

<session-config>
    <session-timeout>30</session-timeout>
</session-config>

不是30我想获得用户偏好时间。在这里我尝试了setmaxinactive interval()方法,但我不知道我必须使用那个

1 个答案:

答案 0 :(得分:0)

获取会话对象后,您可以添加以下属性:

HttpServletRequest req = (HttpServletRequest) request;
HttpSession session = req.getSession(false);
session.setMaxInactiveInterval(60);