UrlValidator is not working for localhost
Asked Answered
P

1

15

I'm facing an issue that i'm using UrlValidator in my code.

UrlValidator urlValidator = UrlValidator.getInstance();
    if(urlValidator.isValid(url)) {
        throw new IllegalArgumentException( url + "not valid");
    }
 where url i'm passing is 

 [1]: http://localhost:8080/myapp/Education.html

.I'm getting IllegalArgumentException n if i'm passing url i.e

 [2]: https://www.google.co.in/

its working fine.How to make this code working for localhost:8080

Picardi answered 8/8, 2014 at 9:2 Comment(0)
A
31

if you take a look at the documentation you'll have a hint on how to accept local urls

For example

public class Validator {
    public static void main(String[] args) {
        UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);
        if (urlValidator.isValid("http://localhost/page.htm")) {
            System.out.println("Valid URL");
        }
        else {
            System.out.println("Invalid URL");
        }

    }

}

it will output

Valid URL

Avivah answered 8/8, 2014 at 9:10 Comment(10)
FYI you need to do new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS)Wheelman
I was hoping he could get to it by himself :-)Avivah
Thanks for replying .But instead of using UrlValidator.getInstance(); i have tried this also "UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);" but still its not working.Picardi
@Avivah : Any comments ?Picardi
yes..what is the condition in the if? if url is valid then you throw an IllegalArgumentException?? tested locally in it worked btwAvivah
But my URL is localhost:8080/myapp/Education.html not localhost/myapp/Education.html. When i'm passing the URL :-localhost/myapp/Education.html not works fine. Actually i have added it into pom.xml filePicardi
this is beacause you are using apache-tomcat on port 8080Avivah
Exactly Alex. So what should be the best approach to overcome with problem. I need it to make this url pass.Picardi
what about using this as an alternative to validate URLs?Avivah
@Avivah Not Working out :(Picardi

© 2022 - 2024 — McMap. All rights reserved.