2

I am trying to read output from JSP file using URLConnection and write it into a html file. Below are the steps I perform:

  1. Created a link (http://mydomain/Export.jsp)
  2. This JSP is divided into three parts
    • Header
    • Body
    • Footer
  3. Header and Footer are static pages and Body data load using AngularJS which makes request to Server.

Problem:

My application opens the connection successfully with JSP page but before loading the page completely. I mean Angular data, URLConnection.getInputStream() gets executed and reads only Header and Footer data.

Below is the code snippet.

URL url = new URL(link);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
InputStreamReader input = new InputStreamReader(inputStream);
File file = new File("output.html");
byte[] buffer = new byte[8 * 1024];
int bytesRead;

try {
    OutputStream output = new FileOutputStream(file);
    try {
        while ((bytesRead = inputStream.read(buffer)) != -1) {
             output.write(buffer, 0, bytesRead);
        }
    } finally {
        output.close();
    }
} finally {
    input.close();
}
cheffe
  • 9,345
  • 2
  • 46
  • 57
Sushil
  • 21
  • 1
  • 1
    AngularJS is client side and your code in in server side. So when you use your code to get data from the url, it's only return the static page. If you wan to load full page like angularjs do, you will need to use some js engine in server side and execute with the static html page. – Tan Mai Van Feb 19 '16 at 06:58
  • 2
    This code reads whatever the JSP sends out, including any embedded JavaScript, but doesn't execute it. – user207421 Feb 19 '16 at 07:03
  • for a clue how this might be done (one has to dig further, however), you might look at http://stackoverflow.com/questions/2961965/accessing-html-generated-by-javascript-with-htmlunit-java – John Donn Feb 19 '16 at 07:25
  • @Tan Mai Van, if I open the link manually it loads angular js content along with static pages. My problem is angular takes time to load the data by that time, static contents get return to URLConnection inputstream. I wanted to know if there is anyway to wait the inputstream untill page is loaded completely? – Sushil Feb 19 '16 at 11:22
  • @Sushil, the issue here URLConnection not trigger JS to run angularJS, so it cannot get data that render from angularJS. Just try to disable JS in your browser and access the site, you will see the same result you get in the output of URLConnection, then you will understand what I said. – Tan Mai Van Feb 22 '16 at 07:00

0 Answers0