forked from laomafeima/WebJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookie.java
More file actions
114 lines (97 loc) · 3.08 KB
/
Cookie.java
File metadata and controls
114 lines (97 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* To change this template, choose Cookie | Templates
* and open the template in the editor.
*/
package Web;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Cookie操作类 maxAge 说明 0表示立即删除,负数表示关闭页面删除,正数表示生存时间
*
* @author Ma
*/
public class Cookie {
public static HashMap RequestCookie;
public static void analysis(String request) {
String[] temp = request.split(";");
Cookie.RequestCookie = new HashMap();
for (int i = 0; i < temp.length; i++) {
String[] item = temp[i].trim().split("\\=");
Cookie.RequestCookie.put(item[0].trim(), item[1].trim());
}
}
/**
* 设置cookie
*/
public static void setCookie(String name, String value, int maxAge, String path, String domain) {
String cookieString = "Set-Cookie: " + name + "=" + Cookie.encode(value) + "; Max-Age=" + maxAge + ";path=" + path + ";domian=" + domain + ";";
Response.Cookies.add(cookieString);
}
/**
* 设置cookie
*/
public static void setCookie(String name, String value, int maxAge, String path) {
String cookieString = "Set-Cookie: " + name + "=" + Cookie.encode(value) + "; Max-Age=" + maxAge + ";path=" + path + ";";
Response.Cookies.add(cookieString);
}
/**
* 设置cookie
*/
public static void setCookie(String name, String value, int maxAge) {
String cookieString = "Set-Cookie: " + name + "=" + Cookie.encode(value) + "; Max-Age=" + maxAge + ";";
Response.Cookies.add(cookieString);
}
/**
* 设置cookie
*/
public static void setCookie(String name, String value) {
String cookieString = "Set-Cookie: " + name + "=" + Cookie.encode(value) + ";";
Response.Cookies.add(cookieString);
}
/**
* 获取Cookie值
*
* @param name
* @return
*/
public static String getCookie(String name) {
if (Cookie.RequestCookie !=null && Cookie.RequestCookie.containsKey(name)) {
String value = (String) Cookie.RequestCookie.get(name);
return Cookie.decode(value);
}else{
return null;
}
}
/**
* 编码字符串
*
* @param encode
* @return
*/
private static String encode(String encode) {
try {
encode = java.net.URLEncoder.encode(encode, "UTF-8");
return encode;
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Cookie.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
/**
* 解码字符串
*
* @param decode
* @return
*/
private static String decode(String decode) {
try {
decode = java.net.URLDecoder.decode(decode, "UTF-8");
return decode;
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Cookie.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}