1 22 package com.geinuke.util; 23 24 25 26 import javax.servlet.http.Cookie ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 31 32 public class CookieHandler { 33 34 protected HttpServletRequest req=null; 35 protected HttpServletResponse res=null; 36 protected Cookie [] reqCookies=null; 37 38 public CookieHandler(HttpServletRequest req,HttpServletResponse res){ 39 this.req=req; 40 this.res=res; 41 reqCookies = req.getCookies(); 42 } 43 44 45 public Cookie getCookie(String key){ 46 Cookie co=null; 47 boolean flag=false; 48 if (this.reqCookies != null) 49 for (int i = 0; i < this.reqCookies.length && !flag; i++) { 50 if (this.reqCookies[i].getName().equals(key)) { 51 co=this.reqCookies[i]; 52 flag=true; 53 } 54 } 55 return co; 56 } 57 58 public String getCookieValue(String key){ 59 String res=null; 60 Cookie co=this.getCookie(key); 61 if(co!=null) 62 res=co.getValue(); 63 return res; 64 } 65 66 67 public void addCookie(String key,String value){ 68 this.addCookie(key,value,-1); 69 } 70 71 public void addCookie(String key,String value,int expire){ 72 Cookie co=new Cookie (key,value); 73 co.setMaxAge(expire); 74 this.res.addCookie(co); 75 } 76 77 public HttpServletRequest getReq() { 78 return req; 79 } 80 public void setReq(HttpServletRequest req) { 81 this.req = req; 82 } 83 public HttpServletResponse getRes() { 84 return res; 85 } 86 public void setRes(HttpServletResponse res) { 87 this.res = res; 88 } 89 } 90 | Popular Tags |