4

A straightforward way to check for URL validity, is to simply handle a MalformedURLException exception:

try {
  URL base = new URL(SomeString);
}
catch (MalformedURLException e) {
  e.printStackTrace();
  // handles this in some way
}

But AFAIK, using exceptions to implement program logic is conceptually incorrect (and perhaps more costly in runtime performance).

On the other hand, I don't know of a Java method that does isValid(String url).

Is there a better way to check URL's string validity without instantiating a URL object (and handling a MalformedURLException)?

Regex Rookie
  • 10,432
  • 15
  • 54
  • 88
  • possible duplicate of [How to check for a valid URL in Java?](http://stackoverflow.com/questions/2230676/how-to-check-for-a-valid-url-in-java). Note that not getting a MalformedURLException is no guarantee that the URL is valid. – Grodriguez Oct 14 '11 at 18:52
  • @Grodriguez Good catch! But the accepted answer there is not acceptable... See performance considerations below. – Regex Rookie Oct 14 '11 at 18:54
  • 1
    worth noting that the hibernate UrlValidator (org.hibernate.validator.constraints.impl) does just catch the exception to ascertain validity. – smp7d Oct 14 '11 at 19:04
  • 1
    Depends on what you consider acceptable :-). Working and slow is better than not working and fast. I'd worry about a proper solution first, then worry about performance only if this proves to be a real bottleneck in your application. For example if after validation the URL will be used to establish a network connection, then the cost of the regex will be absolutely negligible. – Grodriguez Oct 14 '11 at 19:07

2 Answers2

3

Yes, write a regular expression to check it.

Regular expression to match URLs in Java

Community
  • 1
  • 1
smp7d
  • 4,947
  • 2
  • 26
  • 48
  • 4
    A regex is *very* expensive. But is it more expensive than exception handling? ("expensive" here refers to performance) +1 – Regex Rookie Oct 14 '11 at 18:43
  • I'm not sure, that would need some testing (would probably be a good SO question on its own). It does seem to be more elegant though. – smp7d Oct 14 '11 at 18:46
  • I am accepting your answer because after inspecting [the URL Java source code](http://www.docjar.com/html/api/java/net/URL.java.html), I realized that the exception handling may be cheaper. That's because the implementation URL avoids regex. So I think I got all the information that I need, to proceed with a well informed decision. – Regex Rookie Oct 14 '11 at 19:48
2

You can move that functionality to an utility method: URLUtils.isValid(url) where you return false from catch. After that you can switch to another method, if you decide to.

For example you can use UrlValidator from commons-validator.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • The OP seems to be asking for the "better method" to which you're referring. – spork Oct 14 '11 at 18:47
  • @spork - and one exists in the 2nd paragraph of my answer – Bozho Oct 14 '11 at 18:48
  • @Bozho [UrlValidator](http://commons.apache.org/validator/apidocs/org/apache/commons/validator/UrlValidator.html) looks great. I'll check that out. +1 – Regex Rookie Oct 14 '11 at 18:48
  • yup. If only the designers of the URL class have moved the parsing logic to a separate `.isValid(..)` method – Bozho Oct 14 '11 at 18:49
  • @Bozho I just checked [the source code](http://javasourcecode.org/html/open-source/commons-validator/commons-validator-1.3.1/org/apache/commons/validator/UrlValidator.java.html) and they're using regex to perform that the check... I'd better implement that method myself, then. :) – Regex Rookie Oct 14 '11 at 18:53