KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > RedirectTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jstl.el;
31
32 import com.caucho.el.Expr;
33 import com.caucho.jsp.PageContextImpl;
34 import com.caucho.jstl.NameValueTag;
35 import com.caucho.jstl.rt.CoreUrlTag;
36 import com.caucho.util.CharBuffer;
37 import com.caucho.util.L10N;
38
39 import javax.el.ELException;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41 import javax.servlet.jsp.JspException JavaDoc;
42 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
43 import java.io.IOException JavaDoc;
44
45 public class RedirectTag extends TagSupport JavaDoc implements NameValueTag {
46   private static L10N L = new L10N(RedirectTag.class);
47   
48   private Expr _valueExpr;
49   private Expr _contextExpr;
50
51   private CharBuffer _url;
52
53   /**
54    * Sets the URL to be imported.
55    */

56   public void setURL(Expr value)
57   {
58     _valueExpr = value;
59   }
60   
61   /**
62    * Sets the external context for the import.
63    */

64   public void setContext(Expr context)
65   {
66     _contextExpr = context;
67   }
68
69   /**
70    * Adds a parameter.
71    */

72   public void addParam(String JavaDoc name, String JavaDoc value)
73   {
74     String JavaDoc encoding = this.pageContext.getResponse().getCharacterEncoding();
75     
76     CoreUrlTag.addParam(_url, name, value, encoding);
77   }
78
79   public int doStartTag() throws JspException JavaDoc
80   {
81     try {
82       PageContextImpl pageContext = (PageContextImpl) this.pageContext;
83     
84       String JavaDoc value = _valueExpr.evalString(pageContext.getELContext());
85       String JavaDoc context = null;
86
87       if (_contextExpr != null)
88     context = _contextExpr.evalString(pageContext.getELContext());
89     
90       _url = UrlTag.normalizeURL(pageContext, value, context);
91
92       return EVAL_BODY_INCLUDE;
93     } catch (ELException e) {
94       throw new JspException JavaDoc(e);
95     }
96   }
97       
98   public int doEndTag() throws JspException JavaDoc
99   {
100     String JavaDoc value = UrlTag.encodeURL(pageContext, _url);
101
102     try {
103       HttpServletResponse JavaDoc response;
104       ((HttpServletResponse JavaDoc) pageContext.getResponse()).sendRedirect(value);
105     } catch (IOException JavaDoc e) {
106       throw new JspException JavaDoc(e);
107     }
108
109     return SKIP_PAGE;
110   }
111 }
112
Popular Tags