자바 쿠키 어떻게 만 드 는 지...

60951 단어 cookie

java cookie , cookie cookie, cookie cookie 。

cookie, cookie,

?
1
2
3
4
HttpServletRequest request
HttpServletResponse response
Cookie cookie = new Cookie( "cookiename" , "cookievalue" );
response.addCookie(cookie);

cookie,

?
1
2
3
4
5
6
7
8
cookie = new Cookie( "cookiename" , "cookievalue" );
cookie.setMaxAge( 3600 );
// , cookie , cookie
cookie.setPath( "/" );
response.addCookie(cookie);

cookie, cookie

?
1
2
3
4
5
Cookie[] cookies = request.getCookies(); // cookie
for (Cookie cookie : cookies){
cookie.getName(); // get the cookie name
cookie.getValue(); // get the cookie value
}

cookie 。 , cookie, cookie name,value, , , response ,addCookie()

?
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* cookie
* @param response
* @param name cookie
* @param value cookie
* @param maxAge cookie
*/
public static void addCookie(HttpServletResponse response,String name,String value, int maxAge){
Cookie cookie = new Cookie(name,value);
cookie.setPath( "/" );
if (maxAge> 0 ) cookie.setMaxAge(maxAge);
response.addCookie(cookie);
}

cookie , , , cookie name, cookie value, , cookie Map ,

?
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
/**
* cookie
* @param request
* @param name cookie
* @return
*/
public static Cookie getCookieByName(HttpServletRequest request,String name){
Map<String,Cookie> cookieMap = ReadCookieMap(request);
if (cookieMap.containsKey(name)){
Cookie cookie = (Cookie)cookieMap.get(name);
return cookie;
} else {
return null ;
}
}
/**
* cookie Map
* @param request
* @return
*/
private static Map<String,Cookie> ReadCookieMap(HttpServletRequest request){
Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
Cookie[] cookies = request.getCookies();
if ( null !=cookies){
for (Cookie cookie : cookies){
cookieMap.put(cookie.getName(), cookie);
}
}
return cookieMap;
}

좋은 웹페이지 즐겨찾기