View previous topic :: View next topic |
Author |
Message |
iarwain Apprentice
![Apprentice Apprentice](/images/ranks/rank_rect_2.gif)
![](images/avatars/213781051841c08d6ed08fd.jpg)
Joined: 25 Sep 2003 Posts: 253
|
Posted: Mon Oct 09, 2006 4:48 pm Post subject: Postgresql+Tomcat with UTF-8 [SOLVED] |
|
|
Hi,
I have a postgresql database with UTF-8 encoding. I can insert characters from any language (english, japanese, ...) and then they're propery shown in a SQL query (using psql or pgadmin3). My problem comes with Tomcat. Imagine a simple servlet which makes a SQL SELECT query. The result should show Japanese characters, but it shows "????" all the time. Latin characters are properly displayed.
However, if I insert the unicode numbers manually to the database, eg
Code: | INSERT INTO MyTable (id,name) values (1,日); |
then the Japanese character is correctly shown.
Any ideas?? Thank you.
Last edited by iarwain on Tue Oct 10, 2006 9:27 pm; edited 1 time in total |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
tomk Bodhisattva
![Bodhisattva Bodhisattva](/images/ranks/rank-bodhisattva.gif)
![](images/avatars/21003072644c471d218211e.jpg)
Joined: 23 Sep 2003 Posts: 7221 Location: Sat in front of my computer
|
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
iarwain Apprentice
![Apprentice Apprentice](/images/ranks/rank_rect_2.gif)
![](images/avatars/213781051841c08d6ed08fd.jpg)
Joined: 25 Sep 2003 Posts: 253
|
Posted: Tue Oct 10, 2006 9:31 am Post subject: |
|
|
Thank you for your help. I've followed the guide this way, but no success:
1) I've added
Code: | <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> | to my servlet.
2) I've created CharsetFilter.java with the code suggested in that wiki and compiled it. Then I've copied it into /var/lib/tomcat-5.5/shared/classes.
3) And in /etc/tomcat5.5/web.xml:
Code: | <!--CharsetFilter start-->
<filter>
<filter-name>Charset Filter</filter-name>
<filter-class>CharsetFilter</filter-class>
<init-param>
<param-name>requestEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Charset Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--CharsetFilter end--> |
I've created a console version of the servlet and it correctly shows all characters. So I guess there's something wrong with my Tomcat.
Am I missing something? |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
iarwain Apprentice
![Apprentice Apprentice](/images/ranks/rank_rect_2.gif)
![](images/avatars/213781051841c08d6ed08fd.jpg)
Joined: 25 Sep 2003 Posts: 253
|
Posted: Tue Oct 10, 2006 9:25 pm Post subject: |
|
|
I've finally solved it. The filter in Tomcat is indeed necessary, and it works well. But there are 2 things I had to change in the servlet:
1) Every time getWriter() is used, there must be a setContentType declaration. In my case:
Code: | response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter(); |
2) In the html code:
Code: | <meta http-equiv='Content-Type' content='text/html;charset=utf-8'> |
Now the UTF-8 text database queries are correctly displayed in the web-browser. And also the UTF-8 text inserted from the web-browser into the database is saved as it should be
Thanks! |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
|