javaurldecoding

Java URLDecoder throws exception when used with a string containing a %


I have a problem with the URLDecoder of Java. I am escaping a String in JavaScript, and send it to a java servlet. Then I decode the escaped String with the following line:

URLDecoder.decode(request.getParameter("text"), "UTF-8");

This works fine for every special characters I have tried, the only one making problems is the '%'. Everytime I use this character in the string, I get the following exception:

java.lang.IllegalArgumentException: URLDecoder: Incomplete trailing escape (%) pattern
    java.net.URLDecoder.decode(URLDecoder.java:187)
    at.fhv.students.rotter.ajax.count.CountServlet.doGet(CountServlet.java:31)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Is this a known bug? Or is it really my mistake?


Solution

  • It is not a bug. You send a wrong encoded String. The %-sign has to be encoded as %25

    If you call request.getParameter(), I think you get a decoded String.