美文网首页
2018-06-22Tomcat之cookie、session

2018-06-22Tomcat之cookie、session

作者: 培根好吃 | 来源:发表于2018-06-22 23:18 被阅读0次
cookie的购物.png 购物过程.png 显示上次访问时间.png session.png session域.png
//格式化时间格式
        Date date=new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String currentTime = format.format(date);
        //服务器向客户端浏览器写cookie
        Cookie cookie=new Cookie("LastAccessTime", currentTime);
        cookie.setMaxAge(10*60*50);
        response.addCookie(cookie);
        //客户端从浏览器客户端读取请求中的cookie
        String cookieValue=null;
        Cookie[] cookies = request.getCookies();
             //判断是否有我们需要的cookie
        if(cookies!=null) {
            for (Cookie cookie1 : cookies) {
                if(("LastAccessTime").equals(cookie1.getName())) {
                     cookieValue = cookie1.getValue();
                }
            }
        }
            //因为要在页面输入中文,所以要先设置编码格式
        response.setCharacterEncoding("utf-8");
        if(cookieValue!=null) {
            response.getWriter().write(cookieValue);
        }else {
            response.getWriter().write("恭喜您,是第一次访问我们的网站!");
        }

相关文章

网友评论

      本文标题:2018-06-22Tomcat之cookie、session

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