2
* Copyright 1999,2005 The Apache Software Foundation.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.jasper.tagplugins.jstl.core;
20
import org.apache.jasper.compiler.tagplugin.TagPlugin;
21
import org.apache.jasper.compiler.tagplugin.TagPluginContext;
23
public class Redirect implements TagPlugin {
25
public void doTag(TagPluginContext ctxt) {
27
//flag for the existence of the "context"
28
boolean hasContext = ctxt.isAttributeSpecified("context");
30
//names of the temp variables
31
String urlName = ctxt.getTemporaryVariableName();
32
String contextName = ctxt.getTemporaryVariableName();
33
String baseUrlName = ctxt.getTemporaryVariableName();
34
String resultName = ctxt.getTemporaryVariableName();
35
String responseName = ctxt.getTemporaryVariableName();
38
ctxt.generateJavaSource("String " + contextName + " = null;");
40
ctxt.generateJavaSource(contextName + " = ");
41
ctxt.generateAttribute("context");
42
ctxt.generateJavaSource(";");
46
ctxt.generateJavaSource("String " + urlName + " = ");
47
ctxt.generateAttribute("url");
48
ctxt.generateJavaSource(";");
50
//get the raw url according to "url" and "context"
51
ctxt.generateJavaSource("String " + baseUrlName + " = " +
52
"org.apache.jasper.tagplugins.jstl.Util.resolveUrl(" + urlName + ", " + contextName + ", pageContext);");
53
ctxt.generateJavaSource("pageContext.setAttribute" +
54
"(\"url_without_param\", " + baseUrlName + ");");
59
ctxt.generateJavaSource("String " + resultName + " = " +
60
"(String)pageContext.getAttribute(\"url_without_param\");");
61
ctxt.generateJavaSource("pageContext.removeAttribute" +
62
"(\"url_without_param\");");
64
//get the response object
65
ctxt.generateJavaSource("HttpServletResponse " + responseName + " = " +
66
"((HttpServletResponse) pageContext.getResponse());");
68
//if the url is relative, encode it
69
ctxt.generateJavaSource("if(!org.apache.jasper.tagplugins.jstl.Util.isAbsoluteUrl(" + resultName + ")){");
70
ctxt.generateJavaSource(" " + resultName + " = "
71
+ responseName + ".encodeRedirectURL(" + resultName + ");");
72
ctxt.generateJavaSource("}");
75
ctxt.generateJavaSource("try{");
76
ctxt.generateJavaSource(" " + responseName + ".sendRedirect(" + resultName + ");");
77
ctxt.generateJavaSource("}catch(java.io.IOException ex){");
78
ctxt.generateJavaSource(" throw new JspTagException(ex.toString(), ex);");
79
ctxt.generateJavaSource("}");