Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Postgresql+Tomcat with UTF-8 [SOLVED]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
iarwain
Apprentice
Apprentice


Joined: 25 Sep 2003
Posts: 253

PostPosted: Mon Oct 09, 2006 4:48 pm    Post subject: Postgresql+Tomcat with UTF-8 [SOLVED] Reply with quote

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,&#26085);

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
View user's profile Send private message
tomk
Bodhisattva
Bodhisattva


Joined: 23 Sep 2003
Posts: 7221
Location: Sat in front of my computer

PostPosted: Mon Oct 09, 2006 6:03 pm    Post subject: Reply with quote

Try the solutions here:

http://wiki.apache.org/tomcat/Tomcat/UTF-8
_________________
Search | Read | Answer | Report | Strip
Back to top
View user's profile Send private message
iarwain
Apprentice
Apprentice


Joined: 25 Sep 2003
Posts: 253

PostPosted: Tue Oct 10, 2006 9:31 am    Post subject: Reply with quote

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
View user's profile Send private message
iarwain
Apprentice
Apprentice


Joined: 25 Sep 2003
Posts: 253

PostPosted: Tue Oct 10, 2006 9:25 pm    Post subject: Reply with quote

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 :D
Thanks!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum