A short example to show the use of apache.commons.validator.UrlValidator class to validate an URL in Java.
import org.apache.commons.validator.UrlValidator; public class ValidateUrlExample{ public static void main(String[] args) { UrlValidator urlValidator = new UrlValidator(); // valid URL if (urlValidator.isValid("http://www.ziben.com.br")) { System.out.println("url is valid"); } else { System.out.println("url is invalid"); } // invalid URL if (urlValidator.isValid("http://invalidURL^$&%$&^")) { System.out.println("url is valid"); } else { System.out.println("url is invalid"); } } }
Output
url is valid url is invalid