Link to home
Start Free TrialLog in
Avatar of joelettuce
joelettuce

asked on

Connecting to URL (another jsp) and parse info (IN JSP ONLY)

Relatively new to Java.  Understand how to do jsp, have located java code to conect to site using URLConnection object.  Can this java code be placed in jsp?  When I do this I get many errors.  I just want to be able to return the content of the page and parse the information that I want.  If I cannot place only in jsp, how do I compile servlet and call from jsp?

Avatar of kotan
kotan
Flag of Malaysia image

That can be done in jsp.
What is the errors you got?

of course you can place java code in a jsp.. just put the code btw <% .... %>

e.g.

<%
  int myInt = 0;
%>

Now if you are Using URLConnection make sure you are importing the appropriate classes.

<%@ page import="java.net.*,java.io.*" %>

<%
        try {
            URL ee = new URL("https://www.experts-exchange.com/");
            URLConnection conn = ee.openConnection();
            DataInputStream dis = new DataInputStream(conn.getInputStream());
            String inputLine;

            while ((inputLine = dis.readLine()) != null) {
                // code that handles each line as it is read in
            }
            dis.close();
        } catch (MalformedURLException me) {
            // handle bad urls
        } catch (IOException ioe) {
            // handle IO errors when making connection or retrieve info etc
        }
%>

HTH,
CJ
Avatar of joelettuce
joelettuce

ASKER

Kotan, the error I receive is the following:

org.apache.jasper.compiler.ParseException: /home/fantasy/java/scraping.jsp(3,26) Attribute  has no value


cheekyci, when I enter the code you provided into the "Body"?  (Is this correct), I get the same error above.  The for the quick response, should be simple.

Here is the code:

<HTML>

<HEAD>
<jsp:include flush="/true"/  page="/components/header.html"/ >
</HEAD>

<BODY>

<%@ page import="java.net.*,java.io.*" %>

<%
       try {
           URL ee = new URL("https://www.experts-exchange.com/");
           URLConnection conn = ee.openConnection();
           DataInputStream dis = new DataInputStream(conn.getInputStream());
           String inputLine;

Alert(inputLine);

           while ((inputLine = dis.readLine()) != null) {
               // code that handles each line as it is read in
           }
           dis.close();
       } catch (MalformedURLException me) {
           // handle bad urls
       } catch (IOException ioe) {
           // handle IO errors when making connection or retrieve info etc
       }
%>

</BODY>
</HTML>
ASKER CERTIFIED SOLUTION
Avatar of cheekycj
cheekycj
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for the quick response.  The information provided was exactly as required.
Glad I could help and Thanx for the "A".

CJ