1
2
3
4
5 package gboat2.base.bridge.util.http;
6
7 import javax.servlet.http.Cookie;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11
12
13
14
15
16
17 public class CookieUtil {
18
19
20
21
22
23
24
25
26
27
28
29 public static void addCookie(String name, String value, String domain,
30 int maxage, String path, HttpServletResponse response) {
31 Cookie cookie = new Cookie(name, value);
32 if (domain != null) {
33 cookie.setDomain(domain);
34 }
35 cookie.setMaxAge(maxage);
36 cookie.setPath(path);
37 response.addCookie(cookie);
38 }
39
40
41
42
43
44
45
46
47
48 public static void addCookie(String name, String value, String domain,
49 int maxage, HttpServletResponse response) {
50 addCookie(name, value, domain, maxage, "/", response);
51 }
52
53
54
55
56
57
58
59
60 public static String getCookie(HttpServletRequest request, String name) {
61 Cookie[] cookies = request.getCookies();
62 if (cookies == null)
63 return null;
64 for (int i = 0; i < cookies.length; i++) {
65 if (cookies[i].getName().equals(name)) {
66 return cookies[i].getValue();
67 }
68 }
69 return null;
70 }
71 }