0

I have a java web project that use Tomcat as server .My problem is when I want to use UTF-8 characters it don't show them correctly.

I mean when I use the UTF-8 characters and run my servlet the characters in browser are not correct.and as I wand to use the in a xml page the page can't be shown.I did it with both firefox and internet explorer but I saw no change.

can anyone tell me what changes I should do in Tomcat and my project to correct this problem?

anony
  • 247
  • 1
  • 3
  • 11
  • Did you try this yet?: http://stackoverflow.com/questions/88838/how-to-convert-strings-to-and-from-utf8-byte-arrays-in-java – Ossie7 Aug 29 '12 at 11:37
  • yes.See I use jsp and I want to use the characters which the users enter ,in my servlet to make changes and show.and I can't be sure that they use utf-8 or not.If they enter utf-8 the thing which I show them at the next will not be in correct form. – anony Aug 29 '12 at 12:02
  • Where are the characters that aren't properly encoded in UTF-8 originally coming from? Are they in a Java source file, in a JSP or have they been entered by a user into a HTML form? Or does it affect several of these sources? – Codo Aug 29 '12 at 12:40
  • NO...No.I think I can't say what I mean in previous comment.Of course the thing they enter is in utf-8.But my problem is if they enter any characters that I need utf-8 ,like Chinese or Persian, I will have problem.I mean my problem is with utf-8 .I can take it but when I use it in my servlet and put it bake into jsp and web browser it's not in correct shape.I hope you understand what I mean. – anony Aug 29 '12 at 16:58

2 Answers2

1

There are multiple things to look at:

  • jsp encoding should be UTF-8 (the encoding of the file - check that in your IDE/editor)
  • you have to send the appropriate header - response.setCharacterEncoding(..) in a servlet and <%@ page pageEncoding="utf-8" %>
  • if you want to use utf in the URL, you'd need to configure URIEncoding="utf-8" in tomcat's server.xml
  • if you have database access, make sure you are communicating properly with it, by setting utf-8 as the communication encoding. Also, make sure your database is using utf-8
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

I solved this problem by:

  1. Adding a filter to my project to encode the request and response strings to UTF-8 using:

    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    
  2. Configuring URIEncoding="utf-8" in Tomcat's server.xml.

Thanks to all of you who helped me to solve it.

Kkkev
  • 4,716
  • 5
  • 27
  • 43
anony
  • 247
  • 1
  • 3
  • 11