KickJava   Java API By Example, From Geeks To Geeks.

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


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>maxAge</b>, used to set the maxAge of a cookie.
27  * <p>
28  * Must be nested within an <b>addCookie</b> tag.
29  * <p>
30  * The cookie maxAge is set to the content of the body of the <b>maxAge</b> tag.
31  * <p>
32  * JSP Tag Lib Descriptor
33  * <p><pre>
34  * &lt;name&gt;maxAge&lt;/name&gt;
35  * &lt;tagclass&gt;org.apache.taglibs.response.CookieMaxAgeTag&lt;/tagclass&gt;
36  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
37  * &lt;info&gt;Set a cookie maxAge.&lt;/info&gt;
38  * </pre>
39  *
40  * @author Glenn Nielsen
41  */

42
43 public class CookieMaxAgeTag 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 maxAge tag to obtain the cookie maxAge.
58      *
59      * @return SKIP_BODY
60      */

61     public final int doAfterBody() throws JspException
62     {
63         // Use the body of the tag as cookie maxAge
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
// maxAge
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 maxAge tag must be nested inside an addCookie tag.");
77     }
78
79         int maxage = 0;
80         try {
81             maxage = Integer.valueOf(s).intValue();
82         } catch(NumberFormatException JavaDoc e) {
83             throw new JspException("Response tag maxAge value must be an integer.");
84         }
85         
86         // set the cookie maxAge
87
ac.setMaxAge(maxage);
88
89         return SKIP_BODY;
90     }
91 }
92
Popular Tags