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

I have a problem with Java URLDecoder. I avoid String in JavaScript and send it to the Java servlet. Then I decode the escaped string with the following line:

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

This is great for all the special characters I tried, the only thing that makes the problems is "%". Every time I use this character in a 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?

+5
source share
3 answers

It's not a mistake. You are sending the wrong encoded string. The character %must be encoded as%25

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

+6

angular , % . , - request.getParameter(). URL- , % "URLDecoder: Incomplete trailing escape (%) pattern". , % , .

+1

To get the parameter, I wrote

String requestURL=request.getQueryString(); 

so that he gives us the parameters. From this we can use String.substring()to obtain the preferred parameter in case of correction length or single parameter. Then

String decodeValue = URLDecoder.decode(value,"UTF-8"); 

will also receive a meaningful string with the% character.

0
source

All Articles