Cookie&&Session

Cookie&&Session

会话

会话: 既用户打开浏览器,访问多个web资源后,关闭浏览器的过程,称之为会话;

有状态会话

有状态会话:既用户访问过页面后,下次再来访问时,服务器能够识别出用户曾经访问过该页面的过程;

保存会话的两种机制

  1. cookie : 客户端技术
  2. Session : 服务器技术
cookie(客户端技术)
客户端获得cookie过程:
  1. 从请求中拿到cookie信息
  2. 通过服务器响应给客户端
cookie相关方法

cookie是依照键值对存储信息

getName()    //获取键
getValue()   //获取值
setMaxAge()  //设置最大的生存时间
getMaxAge()  //获取最大的生存时间
cookie实例(获取上次访问时间)

1.具体实现类

public class Cook extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        Cookie[] cookies = req.getCookies();
        if (cookies!=null){
            for (Cookie cookie : cookies) {
                if(cookie.getName().equals("loginDate")){
                    long l= Long.parseLong(cookie.getValue());
                    Date date = new Date(l);
                    String lastTime= date.toLocaleString();
                    writer.write("上次登录时间为:"+lastTime);
                }
            }
        }
        Cookie date = new Cookie("loginDate", System.currentTimeMillis() + "");
        resp.addCookie(date);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

  1. web.xml中配置路径映射
   <servlet>
        <servlet-name>Cook</servlet-name>
        <servlet-class>com.javaweb.Cookie.Cook</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Cook</servlet-name>
        <url-pattern>/c</url-pattern>
    </servlet-mapping>

注:第一次访问不会显示时间的,从第二次访问时,显示上次的访问时间

cookie细节
  1. 一个cookie只能保存一个信息
  2. 一个web站点可以给浏览器发送多个cookie,最多存放20个
  3. cookie大小限制为4kb
  4. 一个浏览器的cookie上限为:300个
如何删除cookie
  1. 关闭浏览器,意味着一次会话结束,对应的,cookie也会自动失效;
  2. 通过setMaxAge()方法,设置cookie的有效时间(最大生存时间)为0;
Session(服务器技术)
Session是什么:

在计算机中,尤其是在网络应用中,称为"会话控制"。Session 对象存储特定用户会话所需的属性及配置信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。当用户请求来自应用程序的 Web 页时,如果该用户还没有会话,则 Web 服务器将自动创建一个 Session 对象。当会话过期或被放弃后,服务器将终止该会话。Session 对象最常见的一个用法就是存储用户的首选项。

Session生命周期、作用

生命周期:当客户端访问时,服务器会给用户,创建一个Session对象,该对象只要浏览器没关,便会一直存在
作用:用来存储用户信息

Session与cookie的区别
  1. cookie把用户的数据写给浏览器,由浏览器保存;
  2. Session将用户的数据写到用户的独占的Session中,由服务器保存;
  3. Session对象有服务器创建;
Session实例
  1. Ses类向session中存放信息
public class Ses extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        HttpSession session = req.getSession();
        if(session.isNew()){
            writer.write("新的,不能打");
        }else{
            Object id = session.getAttribute("id");
            writer.write("id:"+id);
        }
        session.setAttribute("id",session.getId());
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

  1. Ses1类或去session“id”对应的值

public class Ses1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        HttpSession session = req.getSession();
        Object id = session.getAttribute("id");
        writer.write("id:"+id);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
  1. Ses2移除session中“id”的信息,并且关闭会话
public class Ses2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        HttpSession session = req.getSession();
        session.removeAttribute("id"); //移除id
        session.invalidate(); //关闭会话
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
如何使会话自动过期

在web.xml中设置Session的session-timeout属性即可
代码实现:

<session-config>
//1分钟后session自动失效
        <session-timeout>1</session-timeout>
</session-config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值