GCookie.java
上传用户:toby834
上传日期:2013-10-21
资源大小:2613k
文件大小:1k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. // created on 2002-2-26 at 14:42
  2. package net.acai.util;
  3. /**
  4.  * Title:        清清网络
  5.  * Description:
  6.  * Copyright:    Copyright (c) 2002
  7.  * Company:      www.SuperSpace.com
  8.  * @author:       SuperSpace
  9.  * @version 1.0
  10.  */
  11. import javax.servlet.http.*;
  12. public class GCookie
  13. {
  14. public static String getCookieValue(HttpServletRequest request,String cookieName,String defaultValue)
  15. {
  16. Cookie cookieList[]=request.getCookies();
  17. if(cookieList==null||cookieName==null)
  18. return "";
  19. for(int i= 0;i<cookieList.length;i++)
  20. {
  21. try{
  22. if(cookieList[i].getName().equals(cookieName))
  23. return java.net.URLDecoder.decode(cookieList[i].getValue(),"GBK");
  24. }
  25. catch(Exception e){
  26. e.printStackTrace();
  27. }
  28. }
  29. return "";
  30. }
  31. public static void setCookie(HttpServletResponse response,String cookieName,String cookieValue)
  32. {
  33. Cookie theCookie=new Cookie(java.net.URLEncoder.encode(cookieName),java.net.URLEncoder.encode(cookieValue));
  34. response.addCookie(theCookie);
  35. }
  36. public static void setCookie(HttpServletResponse response,String cookieName,String cookieValue,int cookieMaxage)
  37. {
  38. Cookie theCookie=new Cookie(java.net.URLEncoder.encode(cookieName),java.net.URLEncoder.encode(cookieValue));
  39. theCookie.setMaxAge(cookieMaxage);
  40. response.addCookie(theCookie);
  41. }
  42. }