As I was going on with my new project using JSF 2.1.2 on glassfish 3.1 I reached a point where I needed to write a ‘UTF-8′ text. As in every project before, I had my tags and configurations ready to handle such cases, but this time all I was getting in my database is a gibberish text like “Ø¹ÙØ”.
Checked the glassfish log file and this next warning was all over the log:
WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /your_webapp, because request parameters have already been read, or ServletRequest.getReader() has already been called
This is warning is described in the glassfish wiki, next is quoting from the linked page:
the web container is complaining about the fact that the web application deployed at /your_webapp is trying to set the request encoding too late in the request’s lifecycle, when it is impossible for the container to honor it.
See the javadocs of javax.servlet.ServletRequest.setCharacterEncoding():
This method must be called prior to reading
request parameters or reading input using getReader().
Otherwise, it has no effect.
The container issues a warning to let you know that a call to ServletRequest.setCharacterEncoding() by your_webapp has no effect and is being ignored.
So something must have been taking control of the request too early before setting the character encoding, I tried removing my libraries one by one, still getting this warning, so I reached to a conclusion that it’s either a bug in the new glassfish 3.1 as this is my first project using it, or it’s a mix up of request handlers by my libraries combined.
I Debugged through the issue and noticed that my request was sent in ‘UTF-8′ encoding and the response is received the same as well, but some way in between it’s losing the encoding.
Anyhow there are two solutions for this dilemma:
1- Too Old:
You have to add a servlet filter that works as a request wrapper and sets both the response and request character encoding to ‘UTF-8′. That does remove the warning, and at the same time you make sure your request is set to ‘UTF-8′ manually, but I don’t like this anymore, I mean come on we have reached a level of world wide development where everything should be by default set to ‘UTF-8′.
glassfish-web.xml - previously sun-web.xml, starting from glassfish 3.0 the configuration file was renamed - add this:
<locale-charset-info> <locale-charset-map locale="your_locale_iso" charset="utf-8"/> <parameter-encoding default-charset="utf-8"/> </locale-charset-info>
This parameter will mostly solve your issue, it solved mine, but as I’ve already told you it’s deprecated.
If it didn’t do the trick, make sure your JSF pages and your database are well configured to handle unicode.
Thanks for my friend Hatem Alimam for the great help in solving this issue.
This post was tested using: JSF 2.1.2, Glassfish 3.1, Richfaces 4.0, Primefaces 3.0.M1