KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > util > CookieHandler


1 /*
2 -- GeiNuke --
3 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
4 http://www.hostingjava.it/-geinuke/
5
6 This file is part of GeiNuke.
7
8    GeiNuke is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    GeiNuke is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GeiNuke; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */

22 package com.geinuke.util;
23
24
25
26 import javax.servlet.http.Cookie JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30
31
32 public class CookieHandler {
33     
34     protected HttpServletRequest JavaDoc req=null;
35     protected HttpServletResponse JavaDoc res=null;
36     protected Cookie JavaDoc[] reqCookies=null;
37     
38     public CookieHandler(HttpServletRequest JavaDoc req,HttpServletResponse JavaDoc res){
39         this.req=req;
40         this.res=res;
41         reqCookies = req.getCookies();
42     }
43     
44     
45     public Cookie JavaDoc getCookie(String JavaDoc key){
46         Cookie JavaDoc 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 JavaDoc getCookieValue(String JavaDoc key){
59         String JavaDoc res=null;
60         Cookie JavaDoc co=this.getCookie(key);
61         if(co!=null)
62             res=co.getValue();
63         return res;
64     }
65     
66     
67     public void addCookie(String JavaDoc key,String JavaDoc value){
68         this.addCookie(key,value,-1);
69     }
70     
71     public void addCookie(String JavaDoc key,String JavaDoc value,int expire){
72         Cookie JavaDoc co=new Cookie JavaDoc(key,value);
73         co.setMaxAge(expire);
74         this.res.addCookie(co);
75     }
76     
77     public HttpServletRequest JavaDoc getReq() {
78         return req;
79     }
80     public void setReq(HttpServletRequest JavaDoc req) {
81         this.req = req;
82     }
83     public HttpServletResponse JavaDoc getRes() {
84         return res;
85     }
86     public void setRes(HttpServletResponse JavaDoc res) {
87         this.res = res;
88     }
89 }
90
Popular Tags