简体   繁体   中英

Tomcat 8 utf-8 configuration on netbeans 8.1 not working

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM