KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
20 import javax.servlet.*;
21 import javax.servlet.http.*;
22 import javax.servlet.jsp.*;
23 import javax.servlet.jsp.tagext.*;
24
25 /**
26  * JSP Tag <b>value</b>, used to set the value of a cookie.
27  * <p>
28  * Must be nested within an <b>addCookie</b> tag.
29  * <p>
30  * The cookie value is set to the content of the body of the <b>value</b> tag.
31  * <p>
32  * JSP Tag Lib Descriptor
33  * <p><pre>
34  * &lt;name&gt;value&lt;/name&gt;
35  * &lt;tagclass&gt;org.apache.taglibs.response.CookieValueTag&lt;/tagclass&gt;
36  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
37  * &lt;info&gt;Set a cookie value.&lt;/info&gt;
38  * </pre>
39  *
40  * @author Glenn Nielsen
41  */

42
43 public class CookieValueTag extends BodyTagSupport
44 {
45
46     /**
47      * Method called at start of tag, just returns EVAL_BODY_TAG
48      *
49      * @return EVAL_BODY_TAG
50      */

51     public final int doStartTag() throws JspException
52     {
53     return EVAL_BODY_TAG;
54     }
55
56     /**
57      * Read the body of the value tag to obtain the cookie value.
58      *
59      * @return SKIP_BODY
60      */

61     public final int doAfterBody() throws JspException
62     {
63         // Use the body of the tag as cookie value
64
BodyContent body = getBodyContent();
65         String JavaDoc s = body.getString().trim();
66         // Clear the body since we only used it as input for the cookie
67
// value
68
body.clearBody();
69
70     // Get the parent addCookie tag
71
AddCookieTag ac = null;
72     try {
73         ac = (AddCookieTag)this.findAncestorWithClass(this,
74         Class.forName("org.apache.taglibs.response.AddCookieTag"));
75     } catch(Exception JavaDoc e) {
76         throw new JspException("Response value tag must be nested inside an addCookie tag.");
77     }
78
79         // set the cookie value
80
ac.setValue(s);
81
82         return SKIP_BODY;
83     }
84 }
85
Popular Tags