Pregunta

I have this link in my Struts2 app:

<a href="/MyApp/My.action?w=%E8%A8%80%E8%91%89&key=6f98f58ce">Link</a>

%E8%A8%80%E8%91%89 is shown as 言葉 in the browser status bar, which is good.

PROBLEM: When clicking on this link, Struts2's HttpRequest receives w as garbled text è¨è (seen with Eclipse debug). w is then printed to the JSP, where it shows as è¨è on the browser.

What is the problem? How can I fix it?

Notes:

  • HTML pages contain <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  • Chrome 16.0.912.63 on Ubuntu 2011.04
¿Fue útil?

Solución

This is a simple filter that will do it. You just need to add this filter to your web.xml before the struts2 filter.

public class CharacterEncodingFilter implements Filter
{
    public void doFilter(ServletRequest request, ServletResponse response,
FilterChain next)
        throws IOException, ServletException
    {
        String encoding = request.getCharacterEncoding();
        if (encoding == null || encoding.length() == 0)
        {
            request.setCharacterEncoding("UTF-8");
        }

        next.doFilter(request, response);
    }

}

not sure though it will work as have not tried it myself :)

Update

even after the above Filter i was also facing the same issue and after some digging it as per my understanding is due to the fact that application server will by default use the ISO 8859-1 character encoding.

i added the following entry in my tomcat server.xml file

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

URIEncoding="UTF-8"  // this is what i added 

and now i am able to see the proper character in my jsp page.same we have to do with java default encoding.

read this great article unicode-how-to-get-characters-right by BalusC.

hope this will help you.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top