Session学习笔记

简介:

Session技术: 服务器能够识别不同的浏览者。

Session一般都是用来保存登陆信息的,跟Cookie不同的是:  
Cookie是保存在浏览器中 而Session是保存在服务器中。  
而且 Session是发送一个Cookie来唯一标识一个浏览器的   
(同一个浏览器打开不同窗体也算 但是 关闭后就不算了)

主要技术:

request.getSession();
setAttrbute("name","会话数据");
getAttribute("会话数据")
java.lang.String getId()  : 得到session编号
2)两个getSession方法:
getSession(true) / getSession()  : 创建或得到session对象。没有匹配的session编号,自动创建新的session对象。
getSession(false):    得到session对象。没有匹配的session编号,返回null
3)void setMaxInactiveInterval(int interval)  : 设置session的有效时间

4)void invalidate()     : 手动销毁session对象

session对象销毁时间:

默认情况30分服务器自动回收
修改session回收时间
全局修改session有效时间
<!-- 修改session全局有效时间:分钟 -->
    <session-config>
        <session-timeout>1</session-timeout>
    </session-config>

如何避免浏览器的JSESSIONID的cookie随着浏览器关闭而丢失的问题

手动发送一个硬盘保存的cookie给浏览器

Cookie c = new Cookie("JSESSIONID",session.getId());
       c.setMaxAge(60*60);
       response.addCookie(c);

举一个 栗子 使用Session保存登陆这信息(不同浏览器或者不同时间段打开的同一浏览器可以有不同信息)
这里写图片描述

首先需要登陆页面 和登陆失败后返回的页面:
登陆:


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/hha/xx" method="POST">
    <input type="text" name="name" />
    <input type="text" name="password" />
    <input type="submit" value="点我" />
</form>
</body>
</html>

失败:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
失败
<a href="/hha/One.html">fanhui</a>
</body>
</html>

后台验证信息:

      
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        if("jjc".equals(password)) {
            HttpSession session=request.getSession();
            session.setAttribute("name", name);
            response.sendRedirect(request.getContextPath()+"/yy");
        }else {
            response.sendRedirect(request.getContextPath()+"/Two.html");
        }
    }

跳转到对应的欢迎部分:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter printWriter=response.getWriter();
        String html="";
        HttpSession session=request.getSession(false);
        if(session==null) {
            response.sendRedirect(request.getContextPath()+"/One.html");
            return;
        }
        String name=(String) session.getAttribute("name");
        if(name==null) {
            response.sendRedirect(request.getContextPath()+"/One.html");
            return;
        }
        html+="name:"+name+",<a href='"+request.getContextPath()+"/One.html'>fanhui</a>";
        printWriter.write(html);
    }

清空保存的属性,而不是session:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession(false);
        if(session==null) {
            response.sendRedirect(request.getContextPath()+"/One.html");
        }
        session.removeAttribute("name");
    }
目录
相关文章
|
3天前
|
存储 开发者
Session 使用详解
Session 使用详解
35 1
|
7月前
|
存储 安全 NoSQL
97 # session
97 # session
19 0
|
3天前
|
存储
|
11月前
|
存储 开发框架 安全
session
session
93 0
|
11月前
|
存储 应用服务中间件
Session浅谈
Session浅谈
50 0
|
存储 SQL 开发框架
关于Session你需要了解这些
各种web框架早已考虑到这个问题,比如asp.net,是支持通过配置文件修改session的存储介质为sql server的,所有机器的会话数据都从同一个数据库读,就不会存在不一致的问题;
174 0
|
存储 应用服务中间件 开发者
session 的钝化和活化 | 学习笔记
快速学习 session 的钝化和活化。
361 0
session 的钝化和活化 | 学习笔记
|
SQL Oracle 关系型数据库