Spring MVC URIEncoding cannot send requestparam correctly

I need to send some request parameters from browser to Spring MVC controller and process them later as method parameters. The problem is that tomcat, I think, has not put the correct encoding for the data passing through the URI. Instead ''

, I have:%D0%9C%D0%91%D0%94%D0%9E%D0%A3+%D0%B4%2F%D1%81%E2%84%969%D1%81.+%D0%95%D0%BB%D0%B8%D0%BE%D0%BD%D0%BA%D0%B0

I am reading about this type of problem which comes from tomcat not having a preset URI encoding.

If you don't mind, I have body encoding in tomcat config web.xml, so yes, I have:

<filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
        <init-param>
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>tru?</param-value>
        </init-param>       
    </filter>

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

      

So I'm curious if I need to tweak something for the container configuration? Thanks you

0


source to share


2 answers


You have to set the URIEncoding attribute for the HTTP connector in a file server.xml

inside the tomcat config file:



<Connector port="8080" URIEncoding="UTF-8" ...  />

      

+1


source


Only one way I found to do this without changing the Tomcat config:

put parameter into form

<form onsubmit="encodeParameter(this.param)">
  <input type="text" name="param" />
  <input type="submit" />
</form>
      

Run codeHide result




and then encode it before sending it to the server

function encodeParameter(param){
  param.value =encodeURIComponent(param.value);
}
      

Run codeHide result


Now on the server you will get the correct string.

0


source







All Articles