I’ve a query regarding JAVA (Reading directly from the URLs). I want to read the contents from URLs. I’ve just implemented a code in JAVA and it works well. But i want that code to be implemented in JSP. I tried to use this on JSP page but it does not read the contents of the URL. Please help me out.
JAVA CODE
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
JSP CODE
<%@ page import="java.sql.*,java.net.*,java.io.*,java.lang.*,java.util.*"%>
<html>
<title></title>
<head></head>
<body>
<%
try{
URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}catch(Exception ex){}
%>
</body>
</html>
I’m using JDK1.5.0_16 and Tomcat Version 3.0
Your mistake in JSP is the following line:
This prints the line to the stdout (the console, logfile, etc), not to the HTTP response.
Use the implicit
outobject referring the response output stream:Or, better, just use JSTL
<c:import>. Scriptlets are namely discouraged since a decade.Don’t forget to upgrade your ancient (that was a understatement…) server first. Given that you’re fiddling with JSPs the oldschool way, I’d also wonder if you’re reading the
right and up-to-date resources while learning JSP.