Get Parameter Encoding

26,056

Solution 1

What about this? Could it help?

In your web.xml:

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.example.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <servlet-name>dispatcher</servlet-name>
    </filter-mapping>

com.example.CharacterEncodingFilter:

public class CharacterEncodingFilter implements Filter {

    protected String encoding;

    public void init(FilterConfig filterConfig) throws ServletException {
        encoding = filterConfig.getInitParameter("encoding");
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        request.setCharacterEncoding(encoding);

        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {
        encoding = null;
    }

}

Solution 2

You're having this problem, because the request differentiates between body encoding and URI encoding. A CharacterEncodingFilter sets the body encoding, but not the URI encoding.

You need to set URIEncoding="UTF-8" as an attribute in all your connectors in your Tomcat server.xml. See here: http://tomcat.apache.org/tomcat-6.0-doc/config/ajp.html

Or, alternatively, you can set useBodyEncodingForURI="True".

If you're using the maven tomcat plugin, just add this parameter:

mvn -Dmaven.tomcat.uriEncoding=UTF-8 tomcat:run

Share:
26,056

Related videos on Youtube

Erik
Author by

Erik

Updated on April 01, 2020

Comments

  • Erik
    Erik about 4 years

    I have a problem using spring mvc and special chars in a GET request. Consider the following method:

    @RequestMapping("/update")
    public Object testMethod(@RequestParam String name) throws IOException {
        }
    

    to which I send a GET request with name containing an "ä" (german umlaut), for instance. It results in spring receiving "ä" because the browser maps "ä" to %C3%A4.

    So, how can I get the correct encoded string my controller?

    Thanks for your help!

    • Artem
      Artem about 13 years
      How is the URL encoded? There's only one standard encoding here: utf-8, %-encoded as needed.
  • Erik
    Erik about 13 years
    I have already tried the filter org.springframework.web.filter.CharacterEncodingFilter which comes with spring and does what you describe. Unfortunately without a result.
  • Rihards
    Rihards about 13 years
    Very strange.. Can't figure out what else it could be.
  • Artem
    Artem about 13 years
    Perhaps the data coming from the browser is not utf-8 because the form page is not utf-8.
  • Erik
    Erik about 13 years
    I am using Tomcat directly without a connector. Can the solution you described still work?
  • stacker
    stacker almost 11 years
    For later readers: I tried this but it works only with POST not with GET.
  • nilsi
    nilsi about 10 years
    I didn't now I would need it on the connector tag as well. Just had it on the context tag before. Thanks alot!
  • Hurda
    Hurda almost 10 years
    Why don§t they have default as UTF-8?
  • Jagger
    Jagger about 9 years
    To all the people using webapp-runner this can be achieved by adding --uri-encoding UTF-8 parameter
  • Silvio Troia
    Silvio Troia almost 9 years
    in server.xml files set <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>

Related