1.springmvc框架中,cookie例子
jsp:
<%-- Created by IntelliJ IDEA. User: 44262 Date: 2019/2/28 Time: 18:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>增加</h1> <form action="/Common/addCookie.do"> 用户名:<input type="text" name="uname"> <br/> 值:<input type="text" name="value"> <input type="submit" value="提交"/> </form> <h1>修改</h1> <form action="/Common/editCookie.do"> 用户名:<input type="text" name="uname"> <br/> 值:<input type="text" name="value"> <input type="submit" value="提交"/> </form> <h1>删除</h1> <form action="/Common/delCookie.do"> 用户名:<input type="text" name="uname"> <br/> <input type="submit" value="提交"/> </form> <h1>查询</h1> <a href="${pageContext.request.contextPath }/Common/showCookies.do">查询</a><br> </body> <scipt> </scipt> </html>
后台:
@RequestMapping("/showCookies.do") @ResponseBody public void showCookies(HttpServletRequest request, HttpServletResponse response) throws IOException { Cookie[] cookies = request.getCookies();//这样便可以获取一个cookie数组 if (null == cookies) { System.out.println("没有cookie========="); } else { for (Cookie cookie : cookies) { System.out.println("name:" + cookie.getName() + ",value:" + cookie.getValue()); } } } @RequestMapping(value = "/addCookie.do", produces = "application/json; charset=utf-8") public void addCookie(HttpServletResponse response, String uname, String value) throws IOException { Cookie cookie = new Cookie(uname.trim(), value.trim()); cookie.setMaxAge(30 * 60);// 设置为30min cookie.setPath("/");//可在同一应用服务器内共享cookie的方法 System.out.println("已添加==============="); response.addCookie(cookie); } //注意一、修改、删除Cookie时,新建的Cookie除value、maxAge之外的所有属性,例如name、path、domain等,都要与原Cookie完全一样。否则,浏览器将视为两个不同的Cookie不予覆盖,导致修改、删除失败。 @RequestMapping(value = "/editCookie.do", produces = "application/json; charset=utf-8") public void editCookie(HttpServletRequest request, HttpServletResponse response, String uname, String value) throws IOException { Cookie[] cookies = request.getCookies(); if (null == cookies) { System.out.println("没有cookie=============="); } else { for (Cookie cookie : cookies) { if (cookie.getName().equals(uname)) { System.out.println("原值为:" + cookie.getValue()); cookie.setValue(value); cookie.setPath("/"); cookie.setMaxAge(30 * 60);// 设置为30min System.out.println("被修改的cookie名字为:" + cookie.getName() + ",新值为:" + cookie.getValue()); response.addCookie(cookie); break; } } } } @RequestMapping(value = "/delCookie.do", produces = "application/json; charset=utf-8") public void delCookie(HttpServletRequest request, HttpServletResponse response, String uname) throws IOException { Cookie[] cookies = request.getCookies(); if (null == cookies) { System.out.println("没有cookie=============="); } else { for (Cookie cookie : cookies) { if (cookie.getName().equals(uname)) { //找到一样的cookie设置value的值 再覆盖 cookie.setValue(null); cookie.setMaxAge(0);// 立即销毁cookie cookie.setPath("/"); System.out.println("被删除的cookie名字为:"+cookie.getName()); response.addCookie(cookie); break; } } } }
2.@CookieValue的作用
@RequestMapping("/testCookie") public String testCookie(@CookieValue(value="name",required=false) String name, @CookieValue(value="age",required=false) Integer age){ System.out.println(name+","+age); return "hello"; }
3.根据时间封装和删除cookie
//设置 */function setCookie(key, value, iDay) { var oDate = new Date(); oDate.setDate(oDate.getDate() + iDay); document.cookie = key + '=' + value + ';expires=' + oDate; } //移除 function removeCookie(key) { setCookie(key, '', -1);//这里只需要把Cookie保质期退回一天便可以删除}function getCookie(key) { var cookieArr = document.cookie.split('; ');
for(var i = 0; i < cookieArr.length; i++) {
var arr = cookieArr[i].split('=');
if(arr[0] === key) {
return arr[1]; } } return false; }