KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > core > RedirectSupport


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.standard.tag.common.core;
18
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.JspTagException JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
24
25 /**
26  * <p>Support for tag handlers for &lt;redirect&gt;, JSTL 1.0's tag
27  * for redirecting to a new URL (with optional query parameters).</p>
28  *
29  * @author Shawn Bayern
30  */

31
32 public abstract class RedirectSupport extends BodyTagSupport JavaDoc
33     implements ParamParent {
34
35     //*********************************************************************
36
// Protected state
37

38     protected String JavaDoc url; // 'url' attribute
39
protected String JavaDoc context; // 'context' attribute
40

41     //*********************************************************************
42
// Private state
43

44     private String JavaDoc var; // 'var' attribute
45
private int scope; // processed 'scope' attr
46
private ParamSupport.ParamManager params; // added parameters
47

48     //*********************************************************************
49
// Constructor and initialization
50

51     public RedirectSupport() {
52     super();
53     init();
54     }
55
56     private void init() {
57     url = var = null;
58     params = null;
59     scope = PageContext.PAGE_SCOPE;
60     }
61
62
63    //*********************************************************************
64
// Tag attributes known at translation time
65

66     public void setVar(String JavaDoc var) {
67         this.var = var;
68     }
69
70     public void setScope(String JavaDoc scope) {
71     this.scope = Util.getScope(scope);
72     }
73
74
75     //*********************************************************************
76
// Collaboration with subtags
77

78     // inherit Javadoc
79
public void addParameter(String JavaDoc name, String JavaDoc value) {
80     params.addParameter(name, value);
81     }
82
83
84     //*********************************************************************
85
// Tag logic
86

87     // resets any parameters that might be sent
88
public int doStartTag() throws JspException JavaDoc {
89     params = new ParamSupport.ParamManager();
90     return EVAL_BODY_BUFFERED;
91     }
92
93
94     // gets the right value, encodes it, and prints or stores it
95
public int doEndTag() throws JspException JavaDoc {
96     String JavaDoc result; // the eventual result
97

98     // add (already encoded) parameters
99
String JavaDoc baseUrl = UrlSupport.resolveUrl(url, context, pageContext);
100         result = params.aggregateParams(baseUrl);
101
102         // if the URL is relative, rewrite it with 'redirect' encoding rules
103
HttpServletResponse JavaDoc response =
104             ((HttpServletResponse JavaDoc) pageContext.getResponse());
105         if (!ImportSupport.isAbsoluteUrl(result))
106             result = response.encodeRedirectURL(result);
107
108     // redirect!
109
try {
110         response.sendRedirect(result);
111     } catch (java.io.IOException JavaDoc ex) {
112         throw new JspTagException JavaDoc(ex.toString(), ex);
113     }
114
115     return SKIP_PAGE;
116     }
117
118     // Releases any resources we may have (or inherit)
119
public void release() {
120     init();
121     }
122 }
123
Popular Tags