涉及到cookie的 setPath问题: https://blog.csdn.net/damys/article/details/49737905
cookie注解:public voic getxxx(@CookieValue(MOBILE_CLIENT) String client, Model model){......}
//获取所有document.cookie的值 document.cookie = "xxx"; //当前页面的静态路径 var cookies2 = document.cookie.split(';');//cookies
//设置cookies function setCookie(name,value) { //写cookies var exp = new Date(); //获取当前时间 var Days = 30; //将exp设置为n天以后的时间 exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); //格式化为cookie识别的时间 document.cookie = name + "=" + value + ";expires=" + exp.toGMTString(); //设置cookie } //得到cookies // function getCookie(name) { // |jack;;| 次写法无法匹配英文括号! // var arr, reg=new RegExp( "(^| )"+name+"=([^;]*)(;|$)" ); // if(arr=document.cookie.match(reg)) return arr[2]; // else return null; // } function getCookie(c_name) { if(document.cookie.length > 0) { var c_start = document.cookie.indexOf(c_name + "=");//获取字符串的起点 if(c_start != -1) { c_start = c_start + c_name.length + 1;//获取值的起点 c_end = document.cookie.indexOf(";", c_start);//获取结尾处 if(c_end == -1) c_end = document.cookie.length;//如果是最后一个,结尾就是cookie字符串的结尾 return decodeURI(document.cookie.substring(c_start, c_end));//截取字符串返回 } } return ""; } //清除所有cookie函数 function clearAllCookie() { var keys = document.cookie.match(/[^ =;]+(?==)/g); if(keys) { for(var i = keys.length; i--;) document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString() } } //去空格 function trimStr(str) { return str.replace(/(^s*)|(s*$)/g, ""); }
注:保存的cookie会自动在每个key前面插入一个空格!(cookie 存储。默认是 一个 键值就隔着一个空格的。。。。 这样的潜规则)
W3school JavaScript cookies教程: http://www.w3school.com.cn/js/js_cookies.asp
cookie乱码 解决: 前台的 encodeURIComponent(value) 配合 后台的 URLDecoder.decode(cookie.getValue(),"UTF-8");
response.setContentType("text/html;charset=UTF-8");
public static void writeCookie(HttpServletResponse response, String key, Object value) throws UnsupportedEncodingException { writeCookie(response, key, ObjectUtils.writeValueAsString(value)); } public static void writeCookie(HttpServletResponse response, String key, String value) throws UnsupportedEncodingException { String urlValue=URLEncoder.encode(value, "utf-8"); Cookie cookie = new Cookie(key, urlValue); // cookie.setPath("/"); cookie.setPath("/quality/public/mobile/qa/classInfo/answer"); //输出的到某个页面// cookie.setHttpOnly(true); cookie.setMaxAge(30 * 24 * 60 * 60); // cookie.setMaxAge(0); response.addCookie(cookie); }