Validating URLs in Java

1. Overview

URL stands for Uniform Resource Locator, and is the address of a unique resource on the web.

In this tutorial, we will discuss the validation of URL using Java. In modern web development, it is very common to read, write, or access a URL through an application. Therefore, a successful verification ensures a valid and compliant URL.

There are various libraries that are used to validate a URL. We will discuss two sections – java.net.Url more from JDK org.apache.commons.validator.routines.UrlValidator From Apache Commons Library.

2. Validate URL Using JDK

Let’s see how we can validate a URL using class java.net.URL,

boolean isValidURL(String url) throws MalformedURLException, URISyntaxException {
    try {
        new URL(url).toURI();
        return true;
    } catch (MalformedURLException e) {
        return false;
    } catch (URISyntaxException e) {
        return false;
    }
}

In the above method, new URL(url).toURI(); tries to make a URI use of string parameter. If string The URL passed is not qualified for syntax, the library throws a Exception,

built-in url class throws Malformed URL Exception When it finds a malformed syntax in the input string Thing. when the format string is not compliant, the built-in class throws a URISyntaxException.

Now, verify that our method works with a little test:

assertTrue(isValidURL("http://baeldung.com/"));
assertFalse(isValidURL("https://www.baeldung.com/ java-%%$^&& iuyi"));

We must understand the difference between URL and URI. turi () The method is important here because it ensures that any URL string that is RC 2396 compliant is converted to url, However, if we use new URL(String value), This will not ensure that the URL created is fully compatible.

Let us see with an example that if we use only new URL(String url)Many non-compliant URLs will pass verification:

boolean isValidUrl(String url) throws MalformedURLException {
    try {
        // it will check only for scheme and not null input 
        new URL(url);
        return true;
    } catch (MalformedURLException e) {
        return false;
    }
}

Let’s see how the above method works for validating different URLs with some tests:

assertTrue(isValidUrl("http://baeldung.com/"));
assertTrue(isValidUrl("https://www.baeldung.com/ java-%%$^&& iuyi")); 
assertFalse(isValidUrl(""));

In the above method, new url (url) Checks only valid protocols and null strings as input. So, if the protocol is true, it will return a url Object even if it does not conform to RC 2396.

Hence, we should use new URL(url).toURI() To make sure the URL is valid and compliant,

3. Verify URL Using Apache Commons

we need to import commons-verifier dependency in our pom.xml file:

<dependency>
    <groupId>commons-validator</groupId>
    <artifactId>commons-validator</artifactId>
    <version>1.7</version>
</dependency>

let’s use url validator To validate a class from this library:

boolean isValidURL(String url) throws MalformedURLException {
    UrlValidator validator = new UrlValidator();
    return validator.isValid(url);
}

In the above method, we make url validator and then use This is correct() Method to check URL validity of string argument.

Let’s see how the above method behaves for different inputs:

assertFalse(isValidURL("https://www.baeldung.com/ java-%%$^&& iuyi"));
assertTrue(isValidURL("http://baeldung.com/"));

url validator Allows us to fix the conditions for validating URL strings. For example, if we use overloaded constructor UrlValidator(string[] plans)It only validates the URL for the provided list of plans (http, HTTPS, ftpe.t.c.).

Similarly there are some other flags too- ALLOW_2_SLASHES, NO_FRAGMENT, And ALLOW_ALL_SCHEMES Which can be set as per the requirement. We can find the details of all the options provided by the library in the official documentation.

4. conclusion

In this article, we learned about two different ways to verify a URL. We also discussed the difference between url(string url) And URL.toURI(),

If we only need to validate protocol and non-null string, we can use url (string url) the creator, However, when we need to verify and pass compliance checks, we need to use URL to URI (url).

Additionally, if we add apache commons dependency, we can use url validator There are additional options available to the class to perform the validation, and to the class to refine the validation rules.

As always, the source code for the examples in this article is available on GitHub.

       

Leave a Comment