request.setCharacterEncoding is not working, and I don’t know why

  Kiến thức lập trình

The server is a jsp, and I sent the following request via curl

curl -X POST 'http://localhost:8080/test.jsp' --header 'Content-Type: application/x-www-form-urlencoded' -d 'name=%B9%DA%C1%D8%BF%B5'

The value of name is encoded with the euc-kr characterset.

And the server is handling it like this

request.setCharacterEncoding("euc-kr");
String name = request.getParameter("name")
System.out.println(name);

But… the characters printed here are broken.

Since the client encoded it as euc-kr and sent it, shouldn’t the server receive it with the request’s characterset changed and the characters should be received normally without breaking?

For reference, the server is tomcat, and the default characterset is utf-8 since I haven’t configured anything else.
The server’s encoding is utf-8, but since I specified the encoding myself with request.setCharacterSet, shouldn’t the characters be unbroken?

  • On the client, we send the URL encoded string.
  • converted to byte array
  • Server encodes the byte array back to a string, which follows the value of request.setCharacterEncoding (or the server’s default if not set separately)

Am I misunderstanding something?
I don’t understand why it’s broken.

LEAVE A COMMENT