KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.*;
20 import javax.servlet.http.*;
21 import javax.servlet.jsp.*;
22 import javax.servlet.jsp.tagext.*;
23
24 /**
25  * JSP Tag <b>cookie</b>, used to output the value of a single
26  * HttpServletRequest cookie with name <b>name</b>.
27  * <p>
28  * JSP Tag Lib Descriptor
29  * <p><pre>
30  * &lt;name&gt;cookie&lt;/name&gt;
31  * &lt;tagclass&gt;org.apache.taglibs.request.CookieTag&lt;/tagclass&gt;
32  * &lt;bodycontent&gt;empty&lt;/bodycontent&gt;
33  * &lt;info&gt;Get the value of a single request cookie.&lt;/info&gt;
34  * &lt;attribute&gt;
35  * &lt;name&gt;name&lt;/name&gt;
36  * &lt;required&gt;true&lt;/required&gt;
37  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
38  * &lt;/attribute&gt;
39  * </pre>
40  *
41  * @author Glenn Nielsen
42  */

43
44 public class CookieTag extends TagSupport
45 {
46     // Name of cookie
47
private String JavaDoc name = null;
48
49     /**
50      * Method called at end of Tag to ouput cookie value
51      *
52      * @return EVAL_PAGE
53      */

54     public final int doEndTag() throws JspException
55     {
56     String JavaDoc value = null;
57
58     // Get the cookies
59
Cookie cookie = null;
60         Cookie [] cookies =
61         ((HttpServletRequest)pageContext.getRequest()).getCookies();
62         if( cookies != null ) {
63             for( int i = 0; i < cookies.length; i++ ) {
64                 if( cookies[i].getName().equals(name) ) {
65                     cookie = cookies[i];
66                     break;
67                 }
68         }
69         }
70         if( cookie != null )
71             value = cookie.getValue();
72
73         if( value == null )
74             value = "";
75  
76         try {
77             pageContext.getOut().write(value);
78         } catch(Exception JavaDoc e) {
79             throw new JspException("IO Error: " + e.getMessage());
80         }
81
82     return EVAL_PAGE;
83     }
84
85     /**
86      * Set the required tag attribute <b>name</b>.
87      *
88      * @param String name of cookie
89      */

90     public final void setName(String JavaDoc str)
91     {
92     name = str;
93     }
94
95 }
96
Popular Tags