KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > utility > basic > IncludeTag


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 package org.apache.taglibs.utility.basic;
17
18 import java.net.URL JavaDoc;
19 import java.net.HttpURLConnection JavaDoc;
20 import java.io.BufferedReader JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import javax.servlet.jsp.*;
23 import javax.servlet.jsp.tagext.*;
24
25 /**
26   * This tag will put the contents of
27   * the specified url.
28   *
29   * Author Mandar Raje.
30   */

31
32 public class IncludeTag extends BodyTagSupport {
33
34     private String JavaDoc url;
35
36     public String JavaDoc getUrl() {
37     return url;
38     }
39
40     public void setUrl(String JavaDoc val) {
41     this.url = val;
42     }
43
44     public int doStartTag() {
45     return EVAL_BODY_TAG;
46     }
47
48     public int doEndTag() throws JspException {
49     try {
50         getDataFromURL(bodyContent);
51         bodyContent.writeOut(bodyContent.getEnclosingWriter());
52     } catch (Exception JavaDoc ex) {
53         throw new JspException(ex.getMessage());
54     }
55     return SKIP_BODY;
56     }
57
58     public void getDataFromURL(BodyContent bodyContent) throws JspException {
59     try {
60         
61         if(getUrl() == null)
62         throw new JspException("URL incorrectly specified");
63         
64         URL JavaDoc includeURL = new URL JavaDoc(getUrl());
65         HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc) includeURL.openConnection();
66         connection.connect();
67         
68         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc
69                            (connection.getInputStream()));
70             String JavaDoc line = null;
71             while ((line = in.readLine()) != null ) {
72         bodyContent.write(line);
73         }
74         
75     } catch (Exception JavaDoc ex) {
76         throw new JspException(ex.getMessage());
77     }
78     }
79 }
80
81
82
83
84
Popular Tags