Ajax encoding problem caused by server provide a different type of encoding such as Windows-31j instead of UTF-8 #4414
Description
In my JSF Facelets application f:ajax is uesed,but error codes happend with japanese characters.
It is easily reproducible.
Here is the reproducer(demoapp).
demoapp.zip
1.index.xhtml
<h:form id="form1"> <h:commandButton value="go to homePage" action="home.xhtml" /> </h:form>
2.home.xhtml
<h:form> <h:inputText id = "inputName" value = "#{userData.name}"> <f:ajax event="focus" render="@this" listener="#{userData.welcomeMessage()}" /> </h:inputText> <h:commandButton id="getCode" value="submit" action="#{userData.getVisible()}" /><br/> <br/> <table width="120" border="0" cellpadding="0" cellspacing="0" style="#{userData.isVisible()}"> <tr> <td> <h:outputText id="outputText" value="#{userData.name}"/> </td> </tr> </table> </h:form>
3.glassfish-web.xml
<parameter-encoding default-charset="Windows-31j"/>
Steps to reproduce:
- deploy the app in GlassFish 4.1.2.
- In browser open indexPage by the following address:
http://ip:port/demoapp/index.xhtml - click "go to homePage" in the indexPage.
- input japanese characters(for example:テスト) in displayed input box.(Ajax request)
- click "submit".
- click "submit" again.
When click "submit" again,you'll see the error codes happen.
By debuging mojarra source,I find that in the step5 the response header content type's charset is set to UTF-8.What I expect is Windows-31j should to be set.
The causes of the error codes are as follows:
In step4,
the ajax request(uri is /home.xhtml) is sent when the data is entered.
Because it's an ajax request,charset=UTF-8 is set in request content type by the following source.
jsf.jf if (req.method === "POST") { if (typeof req.xmlReq.setRequestHeader !== 'undefined') { req.xmlReq.setRequestHeader('Faces-Request', 'partial/ajax'); req.xmlReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); } content = req.queryString; }
moreover,the UTF-8 is cached by the key of its request uri in a method of com.sun.faces.util.ExpiringConcurrentCache#get.
In step5,click "submit" an request(uri is also /home.xhtml) is sent.
The key is also /home.xhtml,so UTF-8 can be got from the cache and then be set to response header content type,and browser's encoder becomes UTF-8.
In step6,when click "submit" again,the default encoding of a request the container uses is Windows-31j not UTF-8,
different from the browser's encoder(UTF-8),so I think the error codes hanppen.
Is it a JSF bug?
Who can give me some thoughts?
Thanks!