Session

作者: 上杉丶零 | 来源:发表于2018-12-21 21:05 被阅读0次

    一、作用

    在浏览器中保存一个任意类型的键值对,通常用来判断用户的登录状态。

    二、配置

    可以使用setMaxInactiveInterval(int interval);设置存活时间,单位为秒,默认为30分钟或服务器关闭。如果设置的值小于或等于0,则表示会话将永远不会超时(失效)。

    三、示例代码

    • 服务器中设置:
    request.getSession().setAttribute("user", user);
    
    • Filter中获取
    User user = (User) request.getSession().getAttribute("user");
    
    • 浏览器中获取
    ${user.nickname}
    
    • 服务器中销毁:
    request.getSession().removeAttribute("user");
    

    四、备注

    • HttpSession session = request.getSession();等同于HttpSession session = request.getSession(true);表示如果当前存在会话则返回该会话,否则新建一个会话
    • HttpSession session = request.getSession(false);表示如果当前存在会话则返回该会话,否则返回null

    相关文章

      网友评论

          本文标题:Session

          本文链接:https://www.haomeiwen.com/subject/yrmykqtx.html