Scripts: Scripts: Java :: using tools :: Library Article #2

Developer's Section

Check if a webpage exists in Java
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Average Votes: (2439)
Rate It:   

learn how to check if a webpage exists in Java.

In this script we will be using the package to figure out if certain webpage exists on the internet. The method exists() first makes sure no redirects are executed. Then it creates an object of type HttpURLConnection to determine if the webpage exists. Finally, it compares the value of getResponseCode() with the static value of HttpURLConnection.HTTP_OK.


 Code Snippet 1


public class testClass {

  public static void main(String args[]) {
  String u = "";
  //test if exists
  boolean answer = exists(u);
  if(answer == true){
      System.out.println("the website:" + u + " exists");
  else {
      System.out.println("the website:" + u + " doesn't exist");

  public static boolean exists(String URLName){
    try {
      HttpURLConnection con =
         (HttpURLConnection) new URL(URLName).openConnection();
      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    catch (Exception e) {       
       return false;

See other Scripts in using tools


Submit Your Scripts:

If you would like to have your Java scripts published in this section please fill out
the form below:
*Your Name or
Home Town:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]


© 2018 Erobo Software, Inc. All Rights Reserved. | Read Privacy Notice | RSS