KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > response > AddCookieTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.response;
18
19 import javax.servlet.*;
20 import javax.servlet.http.*;
21 import javax.servlet.jsp.*;
22 import javax.servlet.jsp.tagext.*;
23
24 /**
25  * JSP Tag <b>addCookie</b>, used to add a cookie to the Http Response.
26  * <p>
27  * The script variable for the <b>name</b> of the cookie is required.
28  * <p>
29  * Cookie attributes such as the comment, value, etc. can be set
30  * statically by using the corresponding addCookie tag attribute.
31  * Or dynamically using the corresponding tag in the body of the
32  * addCookie tag.
33  * <p>
34  * JSP Tag Lib Descriptor
35  * <p><pre>
36  * &lt;name&gt;addCookie&lt;/name&gt;
37  * &lt;tagclass&gt;org.apache.taglibs.response.AddCookieTag&lt;/tagclass&gt;
38  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
39  * &lt;info&gt;Add a cookie to an Http Response.&lt;/info&gt;
40  * &lt;attribute&gt;
41  * &lt;name&gt;name&lt;/name&gt;
42  * &lt;required&gt;true&lt;/required&gt;
43  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
44  * &lt;/attribute&gt;
45  * &lt;attribute&gt;
46  * &lt;name&gt;value&lt;/name&gt;
47  * &lt;required&gt;false&lt;/required&gt;
48  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
49  * &lt;/attribute&gt;
50  * &lt;attribute&gt;
51  * &lt;name&gt;comment&lt;/name&gt;
52  * &lt;required&gt;false&lt;/required&gt;
53  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
54  * &lt;/attribute&gt;
55  * &lt;attribute&gt;
56  * &lt;name&gt;domain&lt;/name&gt;
57  * &lt;required&gt;false&lt;/required&gt;
58  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
59  * &lt;/attribute&gt;
60  * &lt;attribute&gt;
61  * &lt;name&gt;maxAge&lt;/name&gt;
62  * &lt;required&gt;false&lt;/required&gt;
63  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
64  * &lt;/attribute&gt;
65  * &lt;attribute&gt;
66  * &lt;name&gt;path&lt;/name&gt;
67  * &lt;required&gt;false&lt;/required&gt;
68  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
69  * &lt;/attribute&gt;
70  * &lt;attribute&gt;
71  * &lt;name&gt;secure&lt;/name&gt;
72  * &lt;required&gt;false&lt;/required&gt;
73  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
74  * &lt;/attribute&gt;
75  * &lt;attribute&gt;
76  * &lt;name&gt;version&lt;/name&gt;
77  * &lt;required&gt;false&lt;/required&gt;
78  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
79  * &lt;/attribute&gt;
80  * </pre>
81  *
82  * @author Glenn Nielsen
83  */

84
85 public class AddCookieTag extends TagSupport
86 {
87     private Cookie cookie = null;
88     private String JavaDoc name = null;
89     private String JavaDoc value = null;
90     private String JavaDoc comment = null;
91     private String JavaDoc domain = null;
92     private int maxAge = -1;
93     private String JavaDoc path = null;
94     private boolean secure = false;
95     private int version = 0;
96
97     /**
98      * Method called at start of addCookie tag to create cookie
99      *
100      * @return EVAL_BODY_INCLUDE
101      */

102     public final int doStartTag() throws JspException
103     {
104     cookie = new Cookie(name,"");
105     return EVAL_BODY_INCLUDE;
106     }
107
108     /**
109      * Method called at end of Tag used to set cookie attributes
110      * and add the cookie to the Http Response.
111      *
112      * @return EVAL_PAGE
113      */

114     public final int doEndTag() throws JspException
115     {
116     // Set the cookie attribute values
117
if( value != null )
118         cookie.setValue(value);
119     if( comment != null )
120         cookie.setComment(comment);
121     if( domain != null )
122         cookie.setDomain(domain);
123     cookie.setMaxAge(maxAge);
124     cookie.setSecure(secure);
125     cookie.setVersion(version);
126     // Add the cookie
127
((HttpServletResponse)pageContext.getResponse()).addCookie(cookie);
128
129     return EVAL_PAGE;
130     }
131
132     /**
133      * Optional attribute, specifies a comment that describes a cookie's purpose.
134      *
135      * @param String comment
136      */

137     public final void setComment(String JavaDoc com)
138     {
139     comment = com;
140     }
141
142     /**
143      * Optional attribute, specifies the domain within which this
144      * cookie should be presented.
145      *
146      * @param String domain
147      */

148     public final void setDomain(String JavaDoc dom)
149     {
150     domain = dom;
151     }
152
153     /**
154      * Optional attribute, sets the maximum age of the cookie in seconds.
155      *
156      * @param String number of seconds
157      */

158     public final void setMaxAge(int max)
159     {
160         maxAge = max;
161     }
162
163     /**
164      * Required attribute, name of the cookie.
165      *
166      * @param String name
167      */

168     public final void setName(String JavaDoc nam)
169     {
170     name = nam;
171     }
172
173     /**
174      * Optional attribute, specifies a path for the cookie to which
175      * the client should return the cookie.
176      *
177      * @param String path
178      */

179     public final void setPath(String JavaDoc pth)
180     {
181     path = pth;
182     }
183
184     /**
185      * Optional attribute, assigns a value to a cookie.
186      *
187      * @param String value
188      */

189     public final void setValue(String JavaDoc val)
190     {
191     value = val;
192     }
193
194     /**
195      * Optional attribute, indicates to the browser whether the cookie
196      * should only be sent using a secure protocol, such as HTTPS or SSL.
197      * Set to true or false. Default value is false.
198      *
199      * @param String true or false
200      */

201     public final void setSecure(boolean flag)
202     {
203         secure = flag;
204     }
205
206     /**
207      * Optional attribute, sets the version of the cookie protocol
208      * this cookie complies with.
209      *
210      * @param int version number
211      */

212     public final void setVersion(int version)
213     {
214         this.version = version;
215     }
216
217 }
218
Popular Tags