2

Is there a way of opening a full web page using Java, I have to check the time taken in opening full web page in a Java user end application, I have tried this code:

    URL ur = new URL("http://www.google.com/");
    HttpURLConnection yc =(HttpURLConnection) ur.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
    System.out.println(inputLine);
    in.close();

but this gives me the source code of the url... that's useless for me!

as I need to record the time taken by a webpage to load on my desktop by making a Java application.

Everything is on client site!

Dharman
  • 30,962
  • 25
  • 85
  • 135
rahul
  • 205
  • 3
  • 6
  • 12
  • Did you check that: http://stackoverflow.com/questions/1790500/render-html-in-swing-application – home May 04 '12 at 07:46
  • You want to load it in a GUI component properly rendered? – Thihara May 04 '12 at 07:47
  • @home: just let me give a try with ur post... – rahul May 04 '12 at 08:22
  • @Thihara: yes, i need to load the complete GUI of a webpage, no need to display it....just wanna capture the time that would be taken in opening the full GUI... – rahul May 04 '12 at 08:24
  • @rahul see my answer below.. its just Swing GUI components based. Although not perfect rendering it should serve your purpose. – Thihara May 04 '12 at 08:30

4 Answers4

4

Here is a JavaFX based sample:

import java.util.Date;
import javafx.application.Application;
import javafx.beans.value.*;
import javafx.concurrent.Worker.State;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.scene.web.WebEngine;
import javafx.stage.Stage;

/** times the loading of a page in a WebEngine */
public class PageLoadTiming extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) {
    final Label instructions = new Label(
      "Loads a web page into a JavaFX WebEngine.  " +
      "The first page loaded will take a bit longer than subsequent page loads due to WebEngine intialization.\n" +
      "Once a given url has been loaded, subsequent loads of the same url will be quick as url resources have been cached on the client."
    );
    instructions.setWrapText(true);
    instructions.setStyle("-fx-font-size: 14px");

    // configure some controls.
    final WebEngine engine   = new WebEngine();
    final TextField location = new TextField("http://docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm");
    final Button    go       = new Button("Go");
    final Date      start    = new Date();
    go.setOnAction(new EventHandler<ActionEvent>() {
      @Override public void handle(ActionEvent arg0) {
        engine.load(location.getText());
        start.setTime(new Date().getTime());
      }
    });
    go.setDefaultButton(true);
    final Label timeTaken = new Label();

    // configure help tooltips.
    go.setTooltip(new Tooltip("Start timing the load of a page at the entered location."));
    location.setTooltip(new Tooltip("Enter the location whose page loading is to be timed."));
    timeTaken.setTooltip(new Tooltip("Current loading state and time taken to load the last page."));

    // monitor the page load status and update the time taken appropriately.
    engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
      @Override public void changed(ObservableValue<? extends State> state, State oldState, State newState) {
        switch (newState) {
          case SUCCEEDED: timeTaken.setText(((new Date().getTime()) - start.getTime()) + "ms"); break;
          default: timeTaken.setText(newState.toString());  
        }
      }
    });

    // layout the controls.
    HBox controls = HBoxBuilder.create().spacing(10).children(new Label("Location"), location, go, timeTaken).build();
    HBox.setHgrow(location, Priority.ALWAYS);
    timeTaken.setMinWidth(120);

    // layout the scene.
    VBox layout = new VBox(10);
    layout.setStyle("-fx-padding: 10; -fx-background-color: cornsilk; -fx-font-size: 20px;");
    layout.getChildren().addAll(controls, instructions);
    stage.setTitle("Page load timing");
    stage.getIcons().add(new Image("http://icons.iconarchive.com/icons/aha-soft/perfect-time/48/Hourglass-icon.png"));
    stage.setScene(new Scene(layout, 1000, 110));
    stage.show();

    // trigger loading the default page.
    go.fire();
  }
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406
3
  1. You can use Cobra
  2. You can use plain Swing also
  3. You can also use JavaFX to do it like this:

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    import javafx.scene.web.WebView;
    
    public class MyWebView extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(final Stage primaryStage) {
            final WebView wv = new WebView();
            wv.getEngine().load("http://www.google.com");
            primaryStage.setScene(new Scene(wv));
            primaryStage.show();
        }
    }
    
user2428118
  • 7,935
  • 4
  • 45
  • 72
Timmo
  • 3,142
  • 4
  • 26
  • 43
3

Yeah home is right that should do the trick. But this way is much more simpler... Although a bit erroneous it should serve fine for benchmark purposes.

JEditorPane editorPane = new JEditorPane();
editorPane.setPage(new URL("http://www.google.com"));

The scripts are still rendered on the screen if you just use this snippet, although it can be worked around I don't think you will need to bother with that since you need to just benchmark loading time... However the results may be varying very slightly since it doesn't take time to run the JavaScript.

Thihara
  • 7,031
  • 2
  • 29
  • 56
  • yeah its working perfectly fine!! but alas java script is also an essential part!! some web sites are totally dependent on this part!! and i need need perfectness as this is a part of a project where Milli seconds do matter a lot!! – rahul May 04 '12 at 09:33
  • u said that its 'erroneous'... can u enlighten me about wt kind of errors or exception can come in my way?? – rahul May 04 '12 at 09:38
  • Well JavaScript mostly it will be rendered for all to see and since you won't have a JavaScript engine in JEditorPane it won't run JavaScript... – Thihara May 04 '12 at 10:02
  • Possible duplicate. http://stackoverflow.com/questions/817609/how-to-display-html-in-a-java-application You will probably have to get a third party library for the JavaScript stuff.. Why don't you want to use a library anyway?? – Thihara May 04 '12 at 10:12
  • u are getting me wrong, third party library is ok, i was saying that i can't use any other software to that stuff for me(eg explorer)!! – rahul May 05 '12 at 03:10
  • In that case please refer the question link in the above comment. It should guide you well. – Thihara May 05 '12 at 03:31
-1

To open a web page using java , try the following code :

 import java.io.*;
  public class b {
 public static void main(String args[])throws IOException
 {
 String downloadURL="<url>";
    java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();
 try
  {
       java.net.URI myNewLocation = new java.net.URI(downloadURL);
      myNewBrowserDesktop.browse(myNewLocation);
  }
 catch(Exception e)
  {
   }
  }
  }

This will open the web page in your default browser . As for the time taken to load part , have a look here . There are a lot of suggestions there .

Community
  • 1
  • 1
CyprUS
  • 4,159
  • 9
  • 48
  • 93
  • i need to develop a application to capture the time, i cant use any other third party application, opening a web page in browser is not my cup of tea!! – rahul May 04 '12 at 08:17