0

So here is my html code:

<!doctype html>
<html lang="fr">
  ...
  <form action="servlet" method="post">
    <textarea name="content" rows="30" cols="80" spellcheck="false"></textarea>
    <input type="submit" value="SEND" />
  </form>
  ...
</html>

And then the servlet part

String content = request.getParameter("content");
System.out.println(content);

And the problem is that if I type "é" in my textarea, the result is printed as "?". I may be an encoding problem but I can't make it work. I tried to change the character encoding to UTF-8 in my jsp page and then add the following line to my servlet

request.setCharacterEncoding("UTF-8");

But it doesn't change anything. What should I do ? Again, I am a beginner in JSP/Java EE.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
floribon
  • 19,175
  • 5
  • 54
  • 66

2 Answers2

1

This is a series of 4 steps

First you have to configure your web server.

Then you have to force your web app to use UTF-8 encoding for all requests/responses.

Third you have to use JSP page encoding (you already do)

And last you must use HMTL-meta tags

Here is the perfect article for you How to get UTF-8 working in Java webapps?

Community
  • 1
  • 1
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
1

Ok I finally got it working. I tried everything but still got ? instead of é and I suppose it may be my server fault as I don't use Tomcat or whatever but an embedded Jetty, which configuration is not that clear.

So I use JavaScript to send my string properly encoded:

send( encodeURIComponent(txtarea.value) );

And on the server side I decode it with this little function:

java.net.URLDecoder.decode(request.getParameter("content"), "utf-8");
floribon
  • 19,175
  • 5
  • 54
  • 66