设置和获取Cookie - wenbin_ouyang

时间:2024-03-11 17:18:59

设置和获取Cookie

 1、后端设置Cookie

@RequestMapping("/setcookie")
@ResponseBody
public String fun3(String jsonp, HttpServletResponse response) throws UnsupportedEncodingException {
    String name1 = "张三";
    name1 = URLEncoder.encode(name1, "utf-8"); // URL编码
    Cookie c1 = new Cookie("username1", name1);
    response.addCookie(c1);
    
    String name2 = "李四";
    Cookie c2 = new Cookie("username2", name2);
    response.addCookie(c2);
    
    if(jsonp != null && jsonp.length() > 0) { // jsonp请求
        return jsonp + "(" + "{\"name\":\"测试\",\"age\":10}" + ")";
    } else {
        return "{\"name\":\"测试\",\"age\":10}";
    }
}

 

 

2、后端获取Cookie

@RequestMapping("/getcookie")
@ResponseBody
public Object fun2(String jsonp, @CookieValue String username1, 
        @CookieValue String username2, HttpServletRequest req) {
    // 通过注解@CookieValue获取cookie的值会自动进行URL解码
    System.out.println("username1: " + username1); // username1: 张三
    
    // cookie设置的时候没有进行URL编码
    System.out.println("username2: " + username2); // username2: 李四
    
    Cookie[] cookies = req.getCookies();
  if (cookies != null && cookies.length > 0) {
    for (Cookie c : cookies) {   // username1: %E5%BC%A0%E4%B8%89    // username2: 李四   System.out.println(c.getName() + ": " + c.getValue());     }
  }
Map<String, Object> result = new HashMap<>(); result.put("username1", username1); result.put("username2", username2); return result; }

 

 3、前端获取Cookie (使用jquery.cookie.js)

@RequestMapping("/page/{pageName}")
public String fun2(@PathVariable String pageName) {
    return "user/" + pageName;
}

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="../js/jquery.cookie.js"></script>
</head>
<body>
    姓名1: <input id="username1" type="text" name="username1" value=""/>
    姓名2: <input id="username2" type="text" name="username2" value=""/>
</body>

<script type="text/javascript">
    var obj = $("#username1");
    alert($.cookie("username1"));
    obj.val($.cookie("username1"));
    
    var obj = $("#username2");
    alert($.cookie("username2"));
    obj.val($.cookie("username2"));
</script>
</html>

 

  前端使用jquery.cookie.js获取Cookie:

 

 

4、URL编码和解码

package com.oy;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.UUID;
import org.junit.Test;

public class Demo {
    @Test
    public void demo1() throws Exception {
        String name = "张三123abc";
        String s = URLEncoder.encode(name, "utf-8"); // URL编码
        System.out.println(s); // %E5%BC%A0%E4%B8%89123abc

        s = URLDecoder.decode(s, "utf-8"); // URL解码
        System.out.println(s); // 张三123abc
    }
}

 

5、jquery.cookie.js插件的使用

  (1)js中如何往cookie中放值和取值

  (2)jquery.cookie.js——jquery的cookie插件

 

6、js-cookie

  1)https://blog.csdn.net/sleepwalker_1992/article/details/86100967

 

7、JavaScript Cookie

  1)https://blog.csdn.net/sleepwalker_1992/article/details/85787099