本文实例讲述了Java读写Cookie记录的`方法。分享给大家供大家参考。具体如下:
写Cookie ,cookie的value可以使String, list , map,int :
Cookie usernameCookie = new Cookie("username_" + schoolId, encodedUsername);axAge(60 * 60 * 24 * 365);ookie(usernameCookie);//设置useriCookie--用于心教育的猜你喜欢课程Cookie userIdCookie = new Cookie("userId_" + schoolId, userId);axAge(60 * 60 * 24 * 365);ookie(userIdCookie);
读Cookie:
HttpServletRequest request = ((ServletRequestAttributes)equestAttributes())equest();String userId=null;Cookie[] cookies = ookies();//cookies不为空,则清除if(cookies!=null){ for(Cookie cookieTemp : cookies){ String cookieIdentity = ame(); //查找身份串 if(ls("userId_"+schoolId)) { userId=alue(); } }}