javadecodeencodeurldecode

UrlDecoder getParameter not able to decode password containing '%'


I have a java servlet application where I am using URLDecoder to decode the value of password

Here is the part of my code:

Map<String, String[]> paramMap = request.getParameterMap();
Set<String> keySet =  paramMap.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext())
        {
            String key = iterator.next();
            if (!key.equals("_")){
            try {
               String value = URLDecoder.decode(request.getParameter(key),"UTF-8");
               System.out.println("Getting parameter("+ key +" = '" + value +"')");
           }
        }

The value doesn't get decoded if it contains '%'

I tried to use getQueryString() which return strings containing percentage, but it doesn't have method to extract particular parameter

String requestParamValue = URLDecoder.decode(request.getQueryString(),"UTF-8");

This returns:

Request raw param decoded is feature=check&userid=xyz@gmail.com&password=Asp%8]a/Asp%8]a/

Is there any way I can get the url decoded using request.getParameter(key) for the string containing '%'

Thanks


Solution

  • Percent symbol is a reserved character in URL encoding so it must be encoded itself. The servlet should fail with BAD REQUEST.
    The string provided for password parameter should be URL encoded. Doing that in java:

    String result = java.net.URLEncoder.encode("Asp%8]a/Asp%8]a/",java.nio.charset. StandardCharsets.UTF_8.name());
    result ==> "Asp%258%5Da%2FAsp%258%5Da%2F"