KickJava   Java API By Example, From Geeks To Geeks.

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


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

40
41 public class SetHeaderTag 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 setHeader 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     ((HttpServletResponse)pageContext.getResponse()).setHeader(name,s);
70         return SKIP_BODY;
71     }
72
73     /**
74      * Required attribute, name of the header.
75      *
76      * @param String name
77      */

78     public final void setName(String JavaDoc nam)
79     {
80     name = nam;
81     }
82
83 }
84
Popular Tags