1 16 package org.apache.taglibs.utility.basic; 17 18 import java.net.URL ; 19 import java.net.HttpURLConnection ; 20 import java.io.BufferedReader ; 21 import java.io.InputStreamReader ; 22 import javax.servlet.jsp.*; 23 import javax.servlet.jsp.tagext.*; 24 25 31 32 public class IncludeTag extends BodyTagSupport { 33 34 private String url; 35 36 public String getUrl() { 37 return url; 38 } 39 40 public void setUrl(String 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 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 includeURL = new URL (getUrl()); 65 HttpURLConnection connection = (HttpURLConnection ) includeURL.openConnection(); 66 connection.connect(); 67 68 BufferedReader in = new BufferedReader (new InputStreamReader 69 (connection.getInputStream())); 70 String line = null; 71 while ((line = in.readLine()) != null ) { 72 bodyContent.write(line); 73 } 74 75 } catch (Exception ex) { 76 throw new JspException(ex.getMessage()); 77 } 78 } 79 } 80 81 82 83 84 | Popular Tags |