首页 > 其他分享 >Cookie讲解

Cookie讲解

时间:2023-01-13 22:44:26浏览次数:34  
标签:cookies utf resp req Cookie 讲解 out

 给cookie设置有效期

  

 

显示访问时间

public class CookieDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-type","text/html;charset=utf-8");
        Cookie[] cookies = req.getCookies();
        PrintWriter  out= resp.getWriter();
         if (cookies!=null){
             out.write("你上一次访问的时间是:");
             for (int i = 0; i <cookies.length ; i++) {
                 Cookie cookie = cookies[i];
                 if (cookie.getName().equals("lastLoginTime")) {
                     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);
        resp.addCookie(cookie1);
    }
}

 

标签:cookies,utf,resp,req,Cookie,讲解,out
From: https://www.cnblogs.com/guojianglong/p/17050897.html

相关文章