Post与Get方式提交乱码问题解决
2016-11-29 19:57
337 查看
在做项目中,会遇到乱码问题,解决乱码问题需要因情况而定,其中不同的提交方式也有不同的解决方案。
对于POST方式提交出现乱码,在web.xml添加post乱码的filter,在web.xml中加入:
<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>
将编码改成utf-8的格式,可以解决post请求乱码问题。
对于get请求中文参数出现乱码主要有两个解决方案:
1.修改tomcat配置文件添加编码与工程编码一致,代码如下:
<Connector URIEncoding="utf-8"connectionTimeout="20000" port="8080"protocol="HTTP/1.1" redirectPort="8443"/>
2.另一种方法对参数进行重新编码:
String userName new
String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")
因为,tomcat的默认编码格式是ISO8859-1,需要将tomcat编码后的内容按utf-8格式编码。
对于POST方式提交出现乱码,在web.xml添加post乱码的filter,在web.xml中加入:
<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>
将编码改成utf-8的格式,可以解决post请求乱码问题。
对于get请求中文参数出现乱码主要有两个解决方案:
1.修改tomcat配置文件添加编码与工程编码一致,代码如下:
<Connector URIEncoding="utf-8"connectionTimeout="20000" port="8080"protocol="HTTP/1.1" redirectPort="8443"/>
2.另一种方法对参数进行重新编码:
String userName new
String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")
因为,tomcat的默认编码格式是ISO8859-1,需要将tomcat编码后的内容按utf-8格式编码。
相关文章推荐
- 解决用jquery的get或者post提交方式提交到struts2中的action中文乱码问题
- struts2默认能解决get和post提交方式的乱码问题吗?
- Android网络编程之使用get方式向服务端提交数据和乱码问题的解决
- Spring MVC 3 表单中文提交post请求和get请求乱码问题的解决方法
- 解决表单get方式提交时的中文乱码问题
- 找到一篇关于form以post、get方式提交数据中文乱码问题总结
- 解决表单Post方式提交的中文乱码问题
- 表单提交方式由POST改为GET出现乱码的解决
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- java中form以post、get方式提交数据中文乱码问题总结
- Get方式提交中文乱码问题解决
- Web容器下的get和post方式请求的乱码问题解决
- 中文乱码问题解决,过滤器配置,get post提交乱码,filter,struts乱码,jsp乱码
- 解决springmvc的get提交和post提交的中文乱码问题
- Post与Get提交使用过滤器解决中文乱码问题
- servlet的生命周期、解决乱码的方式和get方式提交与post方式提交的区别 学习笔记
- 解决servlet中输出中文到浏览器以及post、get两种提交方法乱码问题
- java中form以post、get方式提交数据中文乱码问题总结
- 表单通过get和post方式提交数据解决乱码的方法
- JSP中,AJAX使用POST方式提交中文乱码问题解决