Solve the garbled problem from the configuration file (use filter --post, modify tomcat configuration file or recode --get)

If you want to go to the Internet through the request and response methods, there are many, I will not list them.

Add post garbled filter in web.xml

 

Add in web.xml :

<!-- post garbled filter-->

	<filter>
		<filter-name>CharacterEncodingFilter</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>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

The above can solve the problem of garbled post request.

There are two solutions for garbled characters in the Chinese parameters of the get request:

 

Modify the tomcat configuration file to add the code consistent with the project code, as follows:

 

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

 

Another way to recode the parameters:

String userName= new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")


 

ISO8859-1 is the default encoding of tomcat, the content encoded by tomcat needs to be encoded by utf - 8 9

The idea comes from the dark horse programmer, it is only used for learning and communication, and the infringement will be deleted.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325584051&siteId=291194637