0

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

user1925483
  • 5
  • 1
  • 3

2 Answers2

4

Your mistake in JSP is the following line:

System.out.println(inputLine);

This prints the line to the stdout (the console, logfile, etc), not to the HTTP response.

Use the implicit out object referring the response output stream:

out.println(inputLine);

Or, better, just use JSTL <c:import>. Scriptlets are namely discouraged since a decade.

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:import url="http://www.oracle.com" />

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. 

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Definitely, i'll keep my java files separately and call the required method as required. I was just trying to run my code directly from JSP page. I just wanted to know how it works if i use it with JSP. – user1925483 Dec 29 '12 at 21:14
0

You can use HttpClient library as its very easy to use for your task For example

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.yahoo.com");
HttpResponse response = client.execute(request);

// Get the response
BufferedReader rd = new BufferedReader
  (new InputStreamReader(response.getEntity().getContent()));

String line = "";
while ((line = rd.readLine()) != null) {
  textView.append(line);
} 

Here is a tutorial

Is it a constraint for you to not implement HttpClient ? Another point I would like to make that putting logic of this kind in JSP scriplet is bad idea, you should use some service class which fetches value from URL and call the same from your JSP. You can use setproperty and getproperty tag to load properties from that external service

<jsp:useBean id="some_identifier" class="Foo.class" />

<jsp:getProperty name="some_identifier" property="SomeProperty" />
Akhilesh
  • 1,400
  • 3
  • 15
  • 20
  • Akhilesh.. thanks for your reply. But bro i am asked to implement it through URL class. Not the HttpClient class. – user1925483 Dec 29 '12 at 20:17
  • an example through using URL class, implemented in JSP, would be great. – user1925483 Dec 29 '12 at 20:19
  • Yes.. its a constraint for me not to use other classes but URL only. By the way, i always do my implementation the way you told me. But, i was just trying to directly run this code from JSP page. Definitely i'll do this way. – user1925483 Dec 29 '12 at 21:11