1

In my earlier post there was a problem with JSF charset handling, but also the other part of the problem was MySQL connection parameters for inserting data into db. The problem was solved.

But, I migrated the same application from JSP to facelets and the same problem happened again. Characters from input fields are replaced when inserting to database (č is replaced with Ä), but data inserted into db from sql scripts with proper charset are displayed correctly. I'm still using registered filter and page templates are used with head meta tag as following:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2">

If I insert into h:form tag the following attribute:

acceptcharset="iso-8859-2"

I get correct characters in Firefox, but not in IE7.

Is there anything else I should do to make it work?

Thanks in advance.

Community
  • 1
  • 1
Vladimir
  • 305
  • 1
  • 7
  • 16

2 Answers2

1

Add the following line to the filter:

response.setContentType("text/html;charset=ISO-8859-2");

Don't use acceptcharset attribute. IE has serious bugs with it.

Also, when you're using a <?xml?> declaration in top of Facelets XHTML page, ensure that it's using the desired charset or just remove the whole declaration, it's not strictly required.

<?xml version="1.0" encoding="ISO-8859-2"?>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for your reply. Unfortunately, it didn't solve my problem. I don't have any problems displaying correct characters. They are read correctly, but I can't write them to db as I should (and it's not db problem since I can write them correctly either through scripts or with acceptcharset attbitute set). – Vladimir Jun 09 '10 at 12:21
  • Check the response headers in the client side with e.g. firebug. – BalusC Jun 09 '10 at 12:27
  • I get the following response headers: Server: Apache-Coyote/1.1 Content-Type: text/html;charset=UTF-8 Transfer-Encoding: chunked Date: Wed, 09 Jun 2010 12:39:37 GMT 200 OK So, charset is not as I set it, but I don't know why. – Vladimir Jun 09 '10 at 12:43
  • 1
    So it's been overriden with `UTF-8` after all. How did you declare your Facelets XHTML page? With ``? Change or just remove this line. – BalusC Jun 09 '10 at 12:51
  • I left it out from the beginning. – Vladimir Jun 09 '10 at 12:59
  • Interesting. This requiers more tests here. Anyway, any reason that you don't want to use UTF-8? It's actually the best choice as of now. – BalusC Jun 09 '10 at 13:03
  • Well, I use already present MySql database beneath with latin2 encoding and I don't know how sorting is going to perform if I switch to another encoding. – Vladimir Jun 09 '10 at 13:13
  • I left out one piece of information which might be important, I'm using JSF 1.2. – Vladimir Jun 09 '10 at 13:21
  • After I added contentType="text/html; charset=ISO-8859-2" to f:view tag, characters were correctly handled in Firefox, but not in IE7. Response headers contained: Content-Type: text/html; charset=UTF-8;charset=ISO-8859-2. – Vladimir Jun 09 '10 at 15:00
  • Yes, *something* is implicitly adding `charset=UTF-8`, I question if it is Facelets. Are you using the latest Facelets version? – BalusC Jun 09 '10 at 15:15
  • I'm using JSF 1.2 and Facelets 1.1.15. – Vladimir Jun 09 '10 at 15:25
  • Unexpectedly the problem was solved by adding to all xhtml files. Although I have done it once BalusC gave the answer, it didn't fix the problem at that moment. But, now it works fine and I don't know if something else I did meanwhile combined with xml declaration fixed it. BalusC, thank you for your effort! – Vladimir Jun 10 '10 at 21:54
  • Apparently you were hotdeploying, but the changed file wasn't reloaded into server's work memory correctly. An explicit server restart should have fixed this. You're welcome! – BalusC Jun 10 '10 at 21:56
0

i think you can see the implementation of org.springframework.web.filter.CharacterEncodingFilter and you can start your tomcat by adding -Dfile.encoding=ISO-8859-2 as jvm arguments

Ashkan
  • 320
  • 2
  • 17
  • He's already doing the same as this filter does and the `-Dfile.encoding` doesn't have any influence on the charset to be used by the client (webbrowser) to encode the POST data in. The `Content-Type` header does, but *something* (Facelets?) is implicitly setting it to UTF-8. – BalusC Jun 09 '10 at 21:39
  • Thank you for your reply. Yes, I do have filter which worked for me before I moved to Facelets. I suppose it has to do something with Facelets, especially I modified my web pages from JSP to Facelets and that's when problems started. Also, if I remove line: com.sun.facelets.FaceletViewHandler from faces-config.xml, response headers contain Content-Type: application/xhtml+xml;charset=ISO-8859-2 – Vladimir Jun 10 '10 at 08:31
  • I came across this post: http://forums.sun.com/thread.jspa?threadID=5421551 I don't know if it is the answer to my question. Hope not. – Vladimir Jun 10 '10 at 15:33