java.lang.IllegalArgumentException with URLDecoder.decode() for %

Asked on July 23, 2014
I am using URLDecoder.decode() in my web application. When this methods get % in parameter value,
it throws java.lang.IllegalArgumentException.

How to handle this exception.

Replied on July 24, 2014
Check the below Java doc of URLDecoder.decode()
It does not accept "%x" it means it can not accept values as "%" or "%abcd".
To handle the IllegalArgumentException, we need to check for such condition.

// A trailing, incomplete byte encoding such as
                    // "%x" will cause an exception to be thrown

        if ((i < numChars) && (c=='%'))
                        throw new IllegalArgumentException(
                         "URLDecoder: Incomplete trailing escape (%) pattern");

When we send data from browser to server, browser does one time encoding
and request.getParameter() does one time decoding and if output of 
request.getParameter() is passed to URLDecoder.decode(), then "%x" values will throw

Write Answer

©2023 | Privacy Policy | Contact Us