cookie详细介绍

1、什么是会话
用户打开浏览器打开多个链接,然后关闭浏览器,这样整个过程就是一个会话。
客服端 服务端
1、服务端给客服端发送一个cookie;
2、客服端给客服端标记一个session;
2、用cookie保存时间

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/html");
            response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        Cookie[] cookies = request.getCookies();//这里返回一个包含coookie的数组
        if(cookies!=null){
        out.write("你上一次访问的时间是:");
            for (int i = 0; i <cookies.length ; i++) {
                Cookie cookie = cookies[i];
               if( cookie.getName().equals("name")){
                   long lastLoginTime = Long.parseLong(cookie.getValue());
                   Date date = new Date(lastLoginTime);
                   out.write(date.toLocaleString());
               }
            }
        }else
        {
            out.write("这是你的第一个访问");
        }
        Cookie cookie1 = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
        cookie1.setMaxAge(24*60*60);
        response.addCookie(cookie1);
    }

3、解决编程中的乱码问题:
URLDecoder.decode(cookie.getvalue(),“utf-8”)//解码
URLDecoder.encode(cookie.getvalue(),“utf-8”)//编码

发布了10 篇原创文章 · 获赞 0 · 访问量 48

猜你喜欢

转载自blog.csdn.net/qq_42015513/article/details/105442215
今日推荐