KickJava   Java API By Example, From Geeks To Geeks.

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


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>setContentType</b>, used to set the content type for an
27  * HTTP Response.
28  * <p>
29  * The content type is set to the content of the body of the <b>setContentType</b> tag.
30  * <p>
31  * JSP Tag Lib Descriptor
32  * <p><pre>
33  * &lt;name&gt;setContentType&lt;/name&gt;
34  * &lt;tagclass&gt;org.apache.taglibs.response.SetContentTypeTag&lt;/tagclass&gt;
35  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
36  * &lt;info&gt;Sets the content type of the response being sent to the client.&lt;/info&gt;
37  * </pre>
38  *
39  * @author Glenn Nielsen
40  */

41
42 public class SetContentTypeTag extends BodyTagSupport
43 {
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 setContentType tag to obtain the content type.
57      *
58      * @return SKIP_BODY
59      */

60     public final int doAfterBody() throws JspException
61     {
62         // Use the body of the tag as content type
63
BodyContent body = getBodyContent();
64         String JavaDoc s = body.getString().trim();
65         // Clear the body since we only used it as input for the
66
// content type
67
body.clearBody();
68
69     pageContext.getResponse().setContentType(s);
70         return SKIP_BODY;
71     }
72
73 }
74
Popular Tags