4 Replies Latest reply on Aug 26, 2013 2:19 AM by ejb3workshop

    Encoding post data on Tomcat 7 using 2.0.3 corrupted

    ejb3workshop

      I am trying to post UTF-8 encoding text to my JSF2.2 application, however the data received does not seem to be encoded correctly.I narrowed this problem down to the weld listener. To illustrate the issue I created a simple jsp page:

       

      <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
        <head>
          <title>Character encoding test page</title>
        </head>
        <body>
          <p>
            Encoding : <%=request.getCharacterEncoding()%>
          </p>
          <p>Data posted to this form was:
            <%
              request.setCharacterEncoding("UTF-8");
              out.print(request.getParameter("mydata"));
            %>
          </p>
          <form method="GET" action="index.jsp">
            <input type="text" name="mydata">
            <input type="submit" value="SubmitGET" />
          </form>
          <form method="POST" action="index.jsp">
            <input type="text" name="mydata">
            <input type="submit" value="SubmitPOST" />
          </form>
          .g. ç,ğ,ö,ş,ı, etc Soße “ Test data ”   
        </body>
      </html>
      
      

       

      So I have a simple web application which only consist of this page, and the weld library in WEB-INF/lib (weld-servlet-2.0.3.Final.jar).

      So far so good. However as soon as I include the weld listener in my web.xml file

       

      <?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">
        <listener>
          <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
        </listener>
        <session-config>
          <session-timeout>
            30
          </session-timeout>
        </session-config>
      </web-app>
      
      
      

       

      The post parameters are corrupted by the listener.

       

      I did enable URIEncoding="UTF-8" in the server.xml file, but this did not make any difference.

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

       

      With the listener enabled data like "Soße" is received as "Soße", however once the listerner is removed everything works as expected.

        • 1. Re: Encoding post data on Tomcat 7 using 2.0.3 corrupted
          luksa

          We are aware of this problem and are trying to find a solution. You can work around the problem by adding a servlet filter that sets the request character encoding before weld tries to read any request parameters. See https://community.jboss.org/thread/230605

          1 of 1 people found this helpful
          • 2. Re: Encoding post data on Tomcat 7 using 2.0.3 corrupted
            ejb3workshop

            I tried adding using the tomcat encoding filter (http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Set_Character_Encoding_Filter) however this didn't make any different. Using the org.jboss.weld.servlet.ConversationFilter instead also didn't work.

             

            <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">
              <filter>
                <filter-name>SetCharacterEncoding</filter-name>
                <filter-class>org.jboss.weld.servlet.ConversationFilter</filter-class>      
              </filter>
              <filter-mapping>
                <filter-name>SetCharacterEncoding</filter-name>
                <url-pattern>*</url-pattern>
              </filter-mapping>
              <listener>
                <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
              </listener>
              <session-config>
                <session-timeout>
                  30
                </session-timeout>
              </session-config>
            </web-app>
            
            • 3. Re: Encoding post data on Tomcat 7 using 2.0.3 corrupted
              luksa

              You need to add both filters. The filter that sets the encoding must be before weld's ConversationFilter.

              • 4. Re: Re: Encoding post data on Tomcat 7 using 2.0.3 corrupted
                ejb3workshop

                Here is the final working web.xml file.

                 

                <?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">
                
                  <filter>
                    <filter-name>SetCharacterEncoding</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>
                    <init-param>
                      <param-name>ignore</param-name>
                      <param-value>false</param-value>
                    </init-param>        
                  </filter>
                  <filter>
                    <filter-name>Conversation</filter-name>
                    <filter-class>org.jboss.weld.servlet.ConversationFilter</filter-class>      
                  </filter>  <filter-mapping>
                    <filter-name>SetCharacterEncoding</filter-name>
                    <url-pattern>/*</url-pattern>
                  </filter-mapping>  
                  <filter-mapping>
                    <filter-name>Conversation</filter-name>
                    <url-pattern>/*</url-pattern>
                  </filter-mapping>
                  <listener>
                    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
                  </listener>
                  <session-config>
                    <session-timeout>
                      30
                    </session-timeout>
                  </session-config>
                </web-app>
                
                

                 

                Thanks for your help