0

When I write data from my index.xhtml input field into database, I get ??? instead of ąčę. I have set my database Collation to utf8_general_ci. I printed out my value and in Tomcat 8 output i already see ??? so I guess it's tomcats problem. I have checked out a lot of guides and other stackoverflow questions/solutions, nothing helped. This is what i have done:In tomcats server.xml i have:

<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8084" protocol="HTTP/1.1" redirectPort="8443"/>

In tomcats web.xml i uncommented

 <filter>
        <filter-name>setCharacterEncodingFilter</filter-name>
        <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <async-supported>true</async-supported>
    </filter>

and

 <filter-mapping>
        <filter-name>setCharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

In netbeans project web.xml i have

 <filter>
        <filter-name>setCharacterEncodingFilter</filter-name>
        <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>    
        </init-param>    
    </filter>
    <filter-mapping>
        <filter-name>setCharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <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>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>   

This is my filter setCharacterEncodingFilter.java

package Servlets;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


public class setCharacterEncodingFilter implements Filter {

  @Override
    public void init(FilterConfig filterConfig)
            throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("text/html; charset=UTF-8");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

in index.xtml i have all code covered

<f:view contentType="text/html" encoding="UTF-8">
<h:head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
</h:head>
     *code*
</f:view>

This is all i could do after reading all problems solving and guides, have i done something wrong?

Immortal
  • 1
  • 6
  • What about the code that puts the data into the database? – getjackx May 02 '17 at 13:46
  • well in manager i have persist method which is created with JSF managed bean and just add method{persist(m);} and in bean i just call manager to add my values like Thing m = new Thing(); m.setValue(value1); ThingManager tm= new ThingManager(); tm.addThing(m); there is no sign of setting utf-8. And if i have to set utf-8 in every bean i create this would be bad. I imagine all my project to be utf-8 not just one field. – Immortal May 02 '17 at 14:14
  • I am just guessing but you may need to encode your value in Java again like `Charset.forName("UTF-8").encode(myString) `. See [this question](http://stackoverflow.com/questions/5729806/encode-string-to-utf-8). – getjackx May 02 '17 at 14:19
  • Tried to use it in some lines, doesn't work ;/ – Immortal May 02 '17 at 14:35

1 Answers1

0

I got the answer. I edited -&useEncoding=true&characterEncoding=UTF-8-

<property name="javax.persistence.jdbc.url" value="jdbc:mysql:url?zeroDateTimeBehavior=convertToNull&amp;useEncoding=true&amp;characterEncoding=UTF-8"/>

and it got fixed

Immortal
  • 1
  • 6