KickJava   Java API By Example, From Geeks To Geeks.

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


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>setIntHeader</b>, used to add a header for the HTTP Response.
27  * <p>
28  * The header value is set to the content of the body of the <b>setIntHeader</b> tag.
29  * <p>
30  * JSP Tag Lib Descriptor
31  * <p><pre>
32  * &lt;name&gt;setIntHeader&lt;/name&gt;
33  * &lt;tagclass&gt;org.apache.taglibs.response.SetIntHeaderTag&lt;/tagclass&gt;
34  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
35  * &lt;info&gt;Set a single HTTP integer header.&lt;/info&gt;
36  * </pre>
37  *
38  * @author Glenn Nielsen
39  */

40
41 public class SetIntHeaderTag extends BodyTagSupport
42 {
43     private String JavaDoc name = null;
44
45     /**
46      * Method called at start of tag, just returns EVAL_BODY_TAG
47      *
48      * @return EVAL_BODY_TAG
49      */

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

60     public final int doAfterBody() throws JspException
61     {
62         // Use the body of the tag as header value
63
BodyContent body = getBodyContent();
64         String JavaDoc s = body.getString().trim();
65         // Clear the body since we only used it as input for the header
66
// value
67
body.clearBody();
68
69     int value = 0;
70     try {
71         value = Integer.valueOf(s).intValue();
72     } catch(NumberFormatException JavaDoc e) {
73         throw new JspException("Response tag setIntHeader: " +
74         e.getMessage());
75     }
76
77     ((HttpServletResponse)pageContext.getResponse()).setIntHeader(name,value);
78         return SKIP_BODY;
79     }
80
81     /**
82      * Required attribute, name of the header.
83      *
84      * @param String name
85      */

86     public final void setName(String JavaDoc nam)
87     {
88     name = nam;
89     }
90
91 }
92
Popular Tags