KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > request > ExistsCookieTag


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.request;
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>existsCookie</b>, used to determine if an HttpServletRequest
27  * cookie exists.
28  * <p>
29  * Includes the body of the tag if the cookie exists.
30  * <p>
31  * You can set the optional tag cookie <b>value</b> to <i>true</i> or
32  * <i>false</i>. The body of the tag is included if existsCookie matches
33  * the value.
34  * <p>
35  * JSP Tag Lib Descriptor
36  * <p><pre>
37  * &lt;name&gt;existsCookie&lt;/name&gt;
38  * &lt;tagclass&gt;org.apache.taglibs.request.ExistsCookieTag&lt;/tagclass&gt;
39  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
40  * &lt;info&gt;Includes the body of the tag if the request cookie exists.&lt;/info&gt;
41  * &lt;attribute&gt;
42  * &lt;name&gt;name&lt;/name&gt;
43  * &lt;required&gt;true&lt;/required&gt;
44  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
45  * &lt;/attribute&gt;
46  * &lt;attribute&gt;
47  * &lt;name&gt;value&lt;/name&gt;
48  * &lt;required&gt;false&lt;/required&gt;
49  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
50  * &lt;/attribute&gt;
51  * </pre>
52  *
53  * @author Glenn Nielsen
54  */

55
56 public class ExistsCookieTag extends TagSupport
57 {
58     // Name of cookie to test for existance
59
private String JavaDoc name = null;
60     // Optional attribute value of existance required
61
private boolean value = true;
62
63     /**
64      * Includes the body of the tag if the request cookie exists.
65      *
66      * @return SKIP_BODY if existsCookie doesn't match value, EVAL_BODY_include if existsCookie matches value
67      */

68     public final int doStartTag() throws JspException
69     {
70     boolean result = false;
71
72         // Get the cookies
73
Cookie cookie = null;
74         Cookie [] cookies =
75             ((HttpServletRequest)pageContext.getRequest()).getCookies();
76         if( cookies != null ) {
77             for( int i = 0; i < cookies.length; i++ ) {
78                 if( cookies[i].getName().equals(name) ) {
79                     cookie = cookies[i];
80                     break;
81                 }
82             }
83         }
84         if( cookie != null )
85         result = true;
86
87     if( value == result )
88         return EVAL_BODY_INCLUDE;
89
90     return SKIP_BODY;
91     }
92
93     /**
94      * Set the required tag attribute <b>name</b>.
95      *
96      * @param String name of cookie
97      */

98     public final void setName(String JavaDoc str)
99     {
100     name = str;
101     }
102
103     /**
104      * Set the optional tag attribute <b>value</b> to true or false.
105      *
106      * @param String true or false
107      */

108     public final void setValue(boolean value)
109     {
110     this.value = value;
111     }
112
113 }
114
Popular Tags