1
2
3 /*
4 * 写cookie
5 */
6 Cookie namecookie = new Cookie("name",name);
7 Cookie passwordcookie = new Cookie("password",password);
8 Cookie optioncookie = new Cookie("option","1");
9
10 //生命周期
11 namecookie.setMaxAge(60*60*24*365);
12 passwordcookie.setMaxAge(60*60*24*365);
13 optioncookie.setMaxAge(60*60*24*365);
14
15 response.addCookie(namecookie);
16 response.addCookie(passwordcookie);
17 response.addCookie(optioncookie);
18
19 /*
20 * 读cookie
21 */
22 Cookie[] cookies = request.getCookies();
23 if(cookies!=null)
24 {
25 String name = "";
26 String password = "";
27 String option = "";
28 for (int i = 0; i < cookies.length; i++)
29 {
30 Cookie c = cookies[i];
31 if(c.getName().equalsIgnoreCase("name"))
32 {
33 name = c.getValue();
34 }
35 else if(c.getName().equalsIgnoreCase("password"))
36 {
37 password = c.getValue();
38 }
39 else if(c.getName().equalsIgnoreCase("option"))
40 {
41 option = c.getValue();
42 }
43 }
44 }