Solved

simple fetch

Posted on 2002-05-31
13
183 Views
Last Modified: 2010-03-31
My friend wrote a jsp script for me and I want to add a small function inside it.

Using <%=url%> variable I need to fetch the contents of that url and to print them to screen.

Can someone help me out?
0
Comment
Question by:eugeneg
  • 7
  • 6
13 Comments
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
why not simply do:

<jsp:include page="<url>" flush="true" />
0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
sorry, forgot to mention that this will only work if url is a resource in your current servlet context.
0
 

Author Comment

by:eugeneg
Comment Utility
how about if its not on my server?
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 total points
Comment Utility
you can try this as well:
<%@ page import="java.net.URL,
                     java.net.URLConnection,
                     java.io.*" %>
<%
BufferedReader in = null;

try {
     URL myURL = new URL ("http://java.sun.com");
     URLConnection con = myURL.openConnection();

     in     = new BufferedReader(new InputStreamReader(con.getInputStream()));

     String s;
     
     // read in result
    while ((s = in.readLine()) != null) {
          out.println(s);
    }
} finally {
     // close stream
     try {
          if (in != null) {     in.close(); }
     } catch (IOException ie) {}
}
%>
0
 

Author Comment

by:eugeneg
Comment Utility
One more thing, i'll need to pass some variables as parameters, something like:
    URL myURL = new URL ("http://java.sun.com?id=ID");

i want to use the id variables that is used though out the whole jsp.
0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
i'm not sure i understand you correctly but if you have a variable ID used in your jsp, you can do something like:

URL myURL = new URL ("http://java.sun.com?id=" + ID);

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:eugeneg
Comment Utility
I'm passing 2 variables and ran into problem:
    URL myURL = new URL ("http://www.script.cgi?id="+id"&pass="+pass);
0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
URL myURL = new URL ("http://www.script.cgi?id=" + id + "&pass=" + pass);
0
 

Author Comment

by:eugeneg
Comment Utility
the code works but the variables arent getting passed. i wonder why.
0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
id and pass variables must be set before calling new URL(...)

can you post your code?
0
 

Author Comment

by:eugeneg
Comment Utility
i see id and pass are set becuase when i do:

         out.println(id);
I get the id.

But for some reason this doesnt work:
URL myURL = new URL ("http://www.script.cgi?id=" + id + "&pass=" + pass);

still trying to figure it out.
0
 

Author Comment

by:eugeneg
Comment Utility
Great answers and helped me figure out my problems until everything worked on my site.
0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
glad you got it working!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now