Koray Tugay Koray Tugay - 4 months ago 20
Java Question

JSF UTF 8 characters not seen ONLY in first form post, jsf, tomcat?

I have a simple JSF form where I submit a username and password and persist to database.

Only in first form submit, I am having problems with UTF-8 characters. When I submit ğğüüçç in first post, I get wrong characters. In the second try, it is alright.

Here is my web.xml and index.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>



<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>

</web-app>


and my index.xhtml :

<!DOCTYPE html>
<html lang="tr"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Title</title>
</h:head>
<h:body>
<div class="container">
<h:form styleClass="form-signin" prependId="false" id="registirationForm">
<h2 class="form-signin-heading">Please Register!</h2>
<h:inputText styleClass="input-block-level" value="#{registirationFormBean.nickname}" />
<h:inputSecret styleClass="input-block-level" value="#{registirationFormBean.password}"/>
<h:commandButton styleClass="btn btn-large btn-primary" value="Register Me!" action="#{registirationFormBean.registerUser}"/>
</h:form>
</div>
</h:body>
</html>


Edit: This is from server.xml in Tomcat

<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true"
URIEncoding="UTF-8"
/>


When I debug my application, in the first post I see values posted:

enter image description here

But in the second time:

enter image description here

Answer

use

<f:view>

and your locale.

    <f:view locale="#{registirationFormBean.locale}" encoding="UTF-8" contentType="text/html"> 
<h:head>
    <title>Title</title>
</h:head>
<h:body>
    <div class="container">
        <h:form styleClass="form-signin" prependId="false" id="registirationForm">
            <h2 class="form-signin-heading">Please Register!</h2>
            <h:inputText styleClass="input-block-level" value="#{registirationFormBean.nickname}" />
            <h:inputSecret styleClass="input-block-level" value="#{registirationFormBean.password}"/>
            <h:commandButton styleClass="btn btn-large btn-primary" value="Register Me!" action="#{registirationFormBean.registerUser}"/>
        </h:form>
    </div>
</h:body>

</f:view>

provide locale object from your bean like .

public Locale getLocale(){
return new Locale("tr", "TR");
}
Comments