Discussions

Web tier: servlets, JSP, Web frameworks: External Page include

  1. External Page include (2 messages)

    Is there anyway ( API or something ) to include external page or url in to JSP.

    I found a import tag in JTSL core tag libaray, because of web server ( IWS 6 sp5 ) I cannot use.

    I would appreciate if you if there is anyway to do it. For reference see <c:import url="http://www.yahoo.com" />

    Thanks

    Threaded Messages (2)

  2. External Page include[ Go to top ]

    The Jakarta IO taglib also performs these kinds of operations:

    <io:http url="http://www.yahoo.com"/>

    You can get it at:

    http://jakarta.apache.org/taglibs/doc/io-doc/intro.html

    Or, you can do the same thing scriptlets using the java.net.URL and java.net.URLConnection classes:

    <%
    URL url = new URL("http://www.yahoo.com");
    URLConnection con = url.openConnection();
    con.connect();
    InputStream is = (InputStream) con.getContent();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line = br.readLine();
    while (line != null) {
      out.println(line);
      line = br.readLine();
    }
    br.close();
    %>

    Better yet, write a utility method to do the above.

    public void printURLcontents (String urlString, JspWriter out) {
      URL url = new URL(urlString);
      URLConnection con = url.openConnection();
      con.connect();
      InputStream is = (InputStream) con.getContent();
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(isr);
      String line = br.readLine();
      while (line != null) {
        out.println(line);
       line = br.readLine();
      }
      br.close();
    }

    Proper error-handling is left as an exercise to the reader.
  3. External Page include[ Go to top ]

    Thanks Paul... Good recommendation !!