request和response编码

response:默认使用iso-8859-1编码进行编码传送数据给客户端。

服务器设置编码使用: setCharacterEncoding("uft-8");
告知浏览器解码格式:setheader("content-type","text/html;charset=utf-8")
两者可以简化为:setcontentType(" text/html;charset=utf-8")
*****response中响应头中的参数,浏览器默认使用gbk解码。上述方式无效
 例如:response.addHeader("Content-Disposition", "attachment;filename="filename");
filename需要转码成gbk编码。 byte[] b = filename.getBytes("gbk2313");
filename = new string(b); 
 
 
request:在地址栏中直接传参数默认使用UTF-8
其它方式提交数据,默认使用当前面页使用的编码格式。
服务器指定编码解码:
服务器对于post方式,可以使用request.setCharacterEncording("utf-8")方式指定解码方式为utf-8
对于get方式,只能使用字符转码。new String(request.getparam("name").getBytes("iso-8859-1"),"utf-8")

猜你喜欢

转载自www.cnblogs.com/sxf2086he/p/8979363.html