4

I have a jsp file where i am collection form values and send it to struts 2 action class through jquery Ajax.

My Ajax function looks like

var DataValues = $("#Form1").serialize();
    alert(DataValues);
    alert(decodeURI(DataValues));
    $.ajax({url: urlPass,
        dataType:datatypepass,
        method:methodpass,
        data:DataValues,
        success: function(data,stat,Xhr){calbackPass(data,stat,Xhr);},
        error:function(xhr, status, error){alert("Error : "+xhr.responseText+" status : "+xhr.status);}
    });

when i decodeurl and alert it the text i correctly encoded and decoded.

when i send it to struts2 through ajax it makes problem.

i have checked the values in Interceptor it shows the value ???????

Interceptor

  public class LoginInterceptor extends AbstractInterceptor implements StrutsStatics
    {
    @Override
        public String intercept(ActionInvocation arg0) throws Exception 
        {
            HttpServletRequest rs=ServletActionContext.getRequest();
            System.out.println(rs.getCharacterEncoding());
            Map session=ActionContext.getContext().getSession();
            Map<String, Object> parameters=ActionContext.getContext().getParameters();
            for(Map.Entry<String, Object> ll:parameters.entrySet())
            {
                String a[]=(String[])ll.getValue();
                for(String b:a)
                {
                    System.out.println(ll.getKey()+" : "+b);
                }   
  }}}}

in my jsp file i have set content type as UTF-8 and in ajax also i have checked with content type but its not working.In tomcat server.xml also i have set content-type as UTF-8

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

any other setting has to be done for this UTF-8

Thanks in advance.

alagusathish
  • 71
  • 1
  • 2
  • 5
  • I also use struts2 and here is what is to be done to get it working : http://stackoverflow.com/questions/138948/how-to-get-utf-8-working-in-java-webapps – coding_idiot Aug 18 '13 at 18:22
  • As per the url * is working fine you Plz try with this char "அழகு" – alagusathish Aug 18 '13 at 20:49
  • UTF-8 doesn't cover all languages, http://stackoverflow.com/questions/6242526/what-languages-does-the-character-encoding-utf-8-support – coding_idiot Aug 19 '13 at 10:52

1 Answers1

6

Add a character encoding filter to your web.xml that intercepts all requests before Struts do.

<filter>
  <filter-name>CharacterEncodingFilter</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>true</param-value>
  </init-param>
</filter>

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

org.apache.catalina.filters.SetCharacterEncodingFilter is available out-of-the-box Tomcat 7.0.20 onwards. Otherwise, just implement your own Filter that sets the character encoding using

Edit: Added / to properly close filter and filter-mapping tags

request.setCharacterEncoding("UTF-8");
rbp
  • 43,594
  • 3
  • 38
  • 31
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89