-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCookie.java
More file actions
43 lines (38 loc) · 1.07 KB
/
MyCookie.java
File metadata and controls
43 lines (38 loc) · 1.07 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
package utils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
public class MyCookie {
public Cookie addCookie(String name,String password){
System.out.println("addCookie start");
try {
//put Chinese to cookie need encode and decode
String codeName= URLEncoder.encode(name, "utf-8");
String codePassword=URLEncoder.encode(password, "utf-8");
Cookie cookie=new Cookie("user",codeName+","+codePassword);
//default maxage is one year
cookie.setMaxAge(60*60*24*30);
return cookie;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public Cookie removeCookie(HttpServletRequest request){
Cookie[] cookies=request.getCookies();
if(cookies==null){
return null;
}
for(Cookie cookie:cookies){
if(cookie.getName().equals("user")){
cookie.setMaxAge(0);
cookie.setValue(null);
System.out.println("removeCookie");
return cookie;
}
}
return null;
}
}