龙空技术网

常用cookie处理方法工具类

爱音乐的程序员小新人 305

前言:

目前小伙伴们对“写入cookie的代码”大致比较关怀,兄弟们都想要知道一些“写入cookie的代码”的相关文章。那么小编同时在网摘上网罗了一些关于“写入cookie的代码””的相关文章,希望你们能喜欢,大家一起来了解一下吧!

复制代码

1 import java.io.UnsupportedEncodingException;

2 import java.net.URLDecoder;

3

4 import javax.servlet.http.Cookie;

5 import javax.servlet.http.HttpServletRequest;

6 import javax.servlet.http.HttpServletResponse;

7

8 /**

9 * 常用cookie处理方法工具类

10 */

11 public class CookieUtil {

12

13 /**

14 * 添加cookie

15 * @param response

16 * @param key cookie主键

17 * @param value cookie值

18 */

19 public static void addCookie(HttpServletResponse response, String key, String value){

20 Cookie cookie = new Cookie(key, value);

21 cookie.setPath("/");// 这个要设置

22 cookie.setMaxAge(60*60*24*30);//保留一个月 以秒为单位

23 response.addCookie(cookie);

24 }

25

26 /**

27 * 删除cookie

28 * @param request

29 * @param response

30 * @param key cookie主键

31 */

32 public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String key){

33 Cookie cookies[] = request.getCookies();

34 if (cookies != null) {

35 for (int i = 0; i < cookies.length; i++) {

36 if (cookies[i].getName().equals(key)) {

37 Cookie cookie = new Cookie(key,null);

38 cookie.setPath("/");//设置成跟写入cookies一样的

39 cookie.setMaxAge(0);

40 response.addCookie(cookie);

41 }

42 }

43 }

44 }

45

46 /**

47 * 取得cookie的值

48 * @param request

49 * @param key cookie主键

50 */

51 public static String getCookieValue(HttpServletRequest request, String key) throws UnsupportedEncodingException{

52 for(Cookie cookie : request.getCookies()){

53 if (cookie.getName().equals(key)) {

54 return URLDecoder.decode(cookie.getValue(), "UTF-8");

55 }

56 }

57 return null;

58 }

59 }

功能:cookie的添加、删除、获取值

1 import java.io.UnsupportedEncodingException;

2 import java.net.URLDecoder;

3

4 import javax.servlet.http.Cookie;

5 import javax.servlet.http.HttpServletRequest;

6 import javax.servlet.http.HttpServletResponse;

7

8 /**

9 * 常用cookie处理方法工具类

10 */

11 public class CookieUtil {

12

13 /**

14 * 添加cookie

15 * @param response

16 * @param key cookie主键

17 * @param value cookie值

18 */

19 public static void addCookie(HttpServletResponse response, String key, String value){

20 Cookie cookie = new Cookie(key, value);

21 cookie.setPath("/");// 这个要设置

22 cookie.setMaxAge(60*60*24*30);//保留一个月 以秒为单位

23 response.addCookie(cookie);

24 }

25

26 /**

27 * 删除cookie

28 * @param request

29 * @param response

30 * @param key cookie主键

31 */

32 public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String key){

33 Cookie cookies[] = request.getCookies();

34 if (cookies != null) {

35 for (int i = 0; i < cookies.length; i++) {

36 if (cookies[i].getName().equals(key)) {

37 Cookie cookie = new Cookie(key,null);

38 cookie.setPath("/");//设置成跟写入cookies一样的

39 cookie.setMaxAge(0);

40 response.addCookie(cookie);

41 }

42 }

43 }

44 }

45

46 /**

47 * 取得cookie的值

48 * @param request

49 * @param key cookie主键

50 */

51 public static String getCookieValue(HttpServletRequest request, String key) throws UnsupportedEncodingException{

52 for(Cookie cookie : request.getCookies()){

53 if (cookie.getName().equals(key)) {

54 return URLDecoder.decode(cookie.getValue(), "UTF-8");

55 }

56 }

57 return null;

58 }

59 }

标签: #写入cookie的代码