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

3
  • 6
    Scriptlet code and empty catch blocks are two of the worst ideas you can have. I'd recommend learning JSTL and starting all over again. Commented Dec 29, 2012 at 19:53
  • 2
    Tomcat 3? Really? Production version is 7. Time to upgrade. tomcat.apache.org Commented Dec 29, 2012 at 20:01
  • @duffymo... hahaha.. definitely, i'll go for version 7. Commented Dec 29, 2012 at 20:18

2 Answers 2

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. 

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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" />

3 Comments

Akhilesh.. thanks for your reply. But bro i am asked to implement it through URL class. Not the HttpClient class.
an example through using URL class, implemented in JSP, would be great.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.