KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
20 import javax.servlet.*;
21 import javax.servlet.http.*;
22 import javax.servlet.jsp.*;
23 import javax.servlet.jsp.tagext.*;
24
25 /**
26  * JSP Tag <b>encodeUrl</b>, used to encode a URL with the session id
27  * if necessary.
28  * <p>
29  * The <b>encodeUrl</b> tag gets the URL to encode from the body of the tag,
30  * and outputs it after encoding it.
31  * <p>
32  * JSP Tag Lib Descriptor
33  * <p><pre>
34  * &lt;name&gt;encodeUrl&lt;/name&gt;
35  * &lt;tagclass&gt;org.apache.taglibs.response.EncodeURLTag&lt;/tagclass&gt;
36  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
37  * &lt;info&gt;Encode a URL with the JSESSIONID if necessary.&lt;/info&gt;
38  * </pre>
39  *
40  * @author Glenn Nielsen
41  */

42
43 public class EncodeURLTag extends BodyTagSupport
44 {
45     private String JavaDoc encoded_url = null;
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 encodeUrl tag to obtain the URL to encode,
59      * then encode the URL and output it back to the page.
60      *
61      * @return SKIP_BODY
62      */

63     public final int doAfterBody() throws JspException
64     {
65         // Use the body of the tag as header value
66
BodyContent body = getBodyContent();
67         String JavaDoc s = body.getString().trim();
68         // Clear the body since we only used it as input for the header
69
// value
70
body.clearBody();
71
72     encoded_url =
73         ((HttpServletResponse)pageContext.getResponse()).encodeURL(s);
74
75         return SKIP_BODY;
76     }
77
78     /**
79      * Output the result of the calculation
80      *
81      * @return EVAL_PAGE
82      */

83     public final int doEndTag() throws JspException
84     {
85     try {
86         pageContext.getOut().write(encoded_url);
87     } catch(Exception JavaDoc e) {
88         throw new JspException("IO Error: " + e.getMessage());
89     }
90     return EVAL_PAGE;
91     }
92
93 }
94
Popular Tags