KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
20 import java.lang.*;
21 import java.lang.reflect.*;
22 import java.util.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25 import javax.servlet.jsp.*;
26 import javax.servlet.jsp.tagext.*;
27
28 /**
29  * JSP Tag <b>setStatus</b>, used to set the status code for the HTTP Response.
30  * <p>
31  * JSP Tag Lib Descriptor
32  * <p><pre>
33  * &lt;name&gt;setStatus&lt;/name&gt;
34  * &lt;tagclass&gt;org.apache.taglibs.response.SetStatusTag&lt;/tagclass&gt;
35  * &lt;bodycontent&gt;empty&lt;/bodycontent&gt;
36  * &lt;info&gt;Set the status code for the HTTP Response.&lt;/info&gt;
37  * &lt;attribute&gt;
38  * &lt;name&gt;status&lt;/name&gt;
39  * &lt;required&gt;true&lt;/required&gt;
40  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
41  * &lt;/attribute&gt;
42  * </pre>
43  *
44  * @author Glenn Nielsen
45  */

46
47 public class SetStatusTag extends TagSupport
48 {
49     private String JavaDoc status;
50
51     /**
52      * Method called at end of tag which sets the HTTP Response status code.
53      *
54      * @return SKIP_BODY
55      */

56     public final int doEndTag() throws JspException
57     {
58         int status_code = 0;
59         HttpServletResponse resp = null;
60         Field ec = null;
61  
62         try {
63             resp = (HttpServletResponse)pageContext.getResponse();
64             Class JavaDoc rc = resp.getClass();
65             ec = rc.getField(status);
66             status_code = ec.getInt(resp);
67         } catch(Exception JavaDoc e) {
68             throw new JspException(
69                 "Response setStatus tag could not find status code: " + status);
70         }
71
72     ((HttpServletResponse)pageContext.getResponse()).setStatus(status_code);
73         return EVAL_PAGE;
74     }
75
76     /**
77      * Required attribute status, set the status code to return.
78      *
79      * The status must be a text string for a status code as
80      * defined in java class HttpServletResponse. For example,
81      * <b>SC_OK</b> to return HTTP status code 200.
82      *
83      * @param String Status Code name, e.g. "SC_OK"
84      */

85     public final void setStatus(String JavaDoc stat)
86     {
87         status = stat;
88     }
89 }
90
Popular Tags