關於Java讀寫Cookie記錄的方法

來源:文萃谷 4.62K

本文實例講述了Java讀寫Cookie記錄的`方法。分享給大家供大家參考。具體如下:

關於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(); } }}

熱門標籤