美文网首页我是程序员;您好程先生;叫我序员就好了
get、post方式传送数据的乱码问题解决方案

get、post方式传送数据的乱码问题解决方案

作者: wswenyue | 来源:发表于2014-10-10 19:53 被阅读963次

get/post方式的编码问题解决方案


提交数据中文乱码问题:

  • post提交方式,想不乱码,只需要设置request对象的编码即可
    注意:客户机数据是以哪种编码提交的,request就应该设什么编码
    程序中设置request编码就可控制
request.setCharacterEncoding("UTF-8");

  • get提交方式,设置request对象的编码是无效的,用户想不乱码,只能手工转换
String data = "??????";//乱码字符串
byte source[]= data.getBytes("iso8859-1");//得到客户机提交的原始数据
data = new String(source,"UTF-8");//解决乱码 

等同于(开发中常用)

data= new String(data.getBytes("iso8859-1","UTF-8"));

  • get方式乱码,还可以通过改变服务器的配置实现

注:开发中不建议使用改服务器的方式解决乱码问题

查看tomcat7的配置文件中的*** HTTP Connector***,我们可以看到下面这些信息

Attribute Description
URIEncoding This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.
useBodyEncodingForURI This specifies if the encoding specified in contentType should be used for URI query parameters, instead of using the URIEncoding. This setting is present for compatibility with Tomcat 4.1.x, where the encoding specified in the contentType, or explicitly set using Request.setCharacterEncoding method was also used for the parameters from the URL. The default value is false.

注解:

URIEncoding:上面的意思是说URL编码如果未被指定,将使用默认(缺省的)ISO-8859-1 编码
useBodyEncodingForURI:useBodyEncodingForURI默认值是"false",如果修改成"true",那么程序中设置(request设置编码)何种编码方式,URL就以何种编码解码。

修改tomcat服务器的配置文件
1.打开配置文件*\tomcat\conf\server.xml
大概在70行左右会有下面的代码

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

方法一:修改URIEncoding(添加该属性即可)

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

方法二:修改useBodyEncodingForURI(添加该属性即可)

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"  useBodyEncodingForURI="true"/>

程序中设置request编码就可控制

request.setCharacterEncoding("UTF-8");

  • 超链接方式带中文数据过来,乱码的问题解决

通过链接(html文件)传值过来比如

 <a href="/day06/servlet/RequestDemo6?name=中国">戳我</a>

这种传值方式是get方式

URL规定:URL地址后面如果跟了中文数据,一定要经过URL编码

html文件这样没法处理,如果这样写需要成jsp页面

相关文章

  • get、post方式传送数据的乱码问题解决方案

    get/post方式的编码问题解决方案 提交数据中文乱码问题: post提交方式,想不乱码,只需要设置reques...

  • HTML表单学习

    一、post与get方式提交数据的区别 post是向服务器传送数据,get是从服务器获取数据。 post是通过HT...

  • Android 基础day05

    Android基础网络第二天 1 post方式提交数据乱码的解决 2 get方式提交数据乱码解决 3 httpcl...

  • Android基础第五天

    Android基础网络第二天 1 post方式提交数据乱码的解决 2 get方式提交数据乱码解决 3 httpcl...

  • request对象

    获得请求参数 *获得一个值 获得多个值 Tomcat7的request乱码问题解决 GET方式乱码 POST乱码方式解决

  • Get和post提交方式的区别

    get与post是两种不同的提交方式: 1.get是从服务器上获取数据,post是向服务器传送数据。 2.get是...

  • PHP面试宝典(更新中...)

    基础部分 1.表单中get和post提交方式的区别 1. GET是从服务器上获取数据,POST是向服务器传送数据。...

  • Struts2 URL 中传递中文

    问题 表单提交中文参数,如果是用post方式的话不会乱码,但如果用get方式,就会是乱码。 解决办法 tomcat...

  • POST和GET请求方式的区别

    POST和GET的区别 1.传送方式:get通过地址栏传输,post通过报文传输。2.传送长度:get参数有长度限...

  • Ajax的Get和Post的区别

    谈Ajax的Get和Post的区别 Get方式:用get方式可传送简单数据,但大小一般限制在1KB下,数据追加到u...

网友评论

    本文标题:get、post方式传送数据的乱码问题解决方案

    本文链接:https://www.haomeiwen.com/subject/zdhptttx.html