JSP: Sanitizing an URL parameter

E

Elena

Hello,

I'm writing a JSP whose result page should allow for redirection to an
URL specified by the caller via a GET parameter.

I'm going to assign that URL to Javascript's "document.location" and
that works, but I'd also like to "sanitize" the URL parameter to make
sure it is an URL instead of a nasty character combination which could
break my application. I'd like to employ a Java regex for that, but I
can't find a ready made regex to match URLs. Any pointer?

Thanks
 
S

Shivasubramanian A

but I'd also like to "sanitize" the URL parameter to make
sure it is an URL instead of a nasty character combination which could
break my application. I'd like to employ a Java regex for that, but I
can't find a ready made regex to match URLs. Any pointer?

I believe you can use the java.net.URL class to test whether the URL
is valid. If the URL throws a MalformedURLException, then its invalid.

Check http://stackoverflow.com/questions/1600291/validating-url-in-java
for more info.

Regards,
Shivasubramanian A
 
E

Elena

I believe you can use the java.net.URL class to test whether the URL
is valid. If the URL throws a MalformedURLException, then its invalid.

Checkhttp://stackoverflow.com/questions/1600291/validating-url-in-java
for more info.

Regards,
Shivasubramanian A

Thank you for your reply.

I did try that, but it allows passing malformed URLs too. Since I've
to embed such URL into a Javascript, I need it to be sanitized.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top