Skip to content
This repository has been archived by the owner on Mar 20, 2021. It is now read-only.

Ajax encoding problem caused by server provide a different type of encoding such as Windows-31j instead of UTF-8 #4414

Open
caim2015 opened this issue Apr 10, 2020 · 1 comment

Comments

@caim2015
Copy link

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:

  1. deploy the app in GlassFish 4.1.2.
  2. In browser open indexPage by the following address:
    http://ip:port/demoapp/index.xhtml
  3. click "go to homePage" in the indexPage.
  4. input japanese characters(for example:テスト) in displayed input box.(Ajax request)
  5. click "submit".
  6. 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!

@caim2015
Copy link
Author

caim2015 commented May 8, 2020

I think the "submit" request's charset should not be affected by the Ajax encoding "charset=UTF-8" .
Who can give me some thoughts?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant