KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
21 import javax.servlet.*;
22 import javax.servlet.http.*;
23 import javax.servlet.jsp.*;
24 import javax.servlet.jsp.tagext.*;
25
26 /**
27  * JSP Tag <b>sendRedirect</b>, used to send a redirect to a URL
28  * as the HTTP Response.
29  * <p>
30  * The <b>sendRedirect</b> tag gets the URL to redirect to from the body
31  * of the tag.
32  * <p>
33  * JSP Tag Lib Descriptor
34  * <p><pre>
35  * &lt;name&gt;sendRedirect&lt;/name&gt;
36  * &lt;tagclass&gt;org.apache.taglibs.response.SendRedirectTag&lt;/tagclass&gt;
37  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
38  * &lt;info&gt;Send a redirect to a URL as the HTTP Response.&lt;/info&gt;
39  * </pre>
40  *
41  * @author Glenn Nielsen
42  */

43
44 public class SendRedirectTag extends BodyTagSupport
45 {
46
47     /**
48      * Method called at start of tag, just returns EVAL_BODY_TAG
49      *
50      * @return EVAL_BODY_TAG
51      */

52     public final int doStartTag() throws JspException
53     {
54     return EVAL_BODY_TAG;
55     }
56
57     /**
58      * Read the body of the sendRedirect tag to obtain the URL to redirect to.
59      *
60      * @return SKIP_BODY
61      */

62     public final int doAfterBody() throws JspException
63     {
64         // Use the body of the tag as URL to redirect to
65
BodyContent body = getBodyContent();
66         String JavaDoc s = body.getString().trim();
67         // Clear the body since we only used it as input for the URL
68
// value
69
body.clearBody();
70
71     try {
72         ((HttpServletResponse)pageContext.getResponse()).sendRedirect(s);
73     } catch(IOException e) {
74         throw new JspException(
75         "Response sendRedirect tag could not return the redirect: " +
76         e.getMessage());
77     } catch(IllegalStateException JavaDoc e) {
78         throw new JspException(
79         "Response sendRedirect tag could not return the redirect: " +
80         e.getMessage());
81     }
82
83         return SKIP_BODY;
84     }
85
86     /**
87      * Method called at end of Tag used to send redirect,
88      * always skips the remainder of the JSP page.
89      *
90      * @return SKIP_PAGE
91      */

92     public final int doEndTag() throws JspException
93     {
94     return SKIP_PAGE;
95     }
96
97 }
98
Popular Tags