response和request编码
来源:互联网 发布:修改朋友圈定位软件 编辑:程序博客网 时间:2024/05/18 03:58
编码
常见字符编码:iso-8859-1(不支持中文)、gb2312 gbk gb18030(系统默认编码,中国的国标码)
utf-8(万国码,支持全世界的编码,所以我们使用这个)
1.响应编码
当使用response.getWrite()向客户端发送字符数据时候,如果在之前没有设置编码,那么默认使用iso,因为iso不支持中文,所以一定会乱码。
在使用response.getWrite()之前可以使用response.setCharacterEncoding()来设置字符流的编码为gbk或utf-8,当然我们通常会选择utf-8,这样使用response。
在使用response.getWrite()之前可以使用response.setHeader(“Content-type”,”text/html;charset=utf-8”)来设置响应头,通知浏览器服务器这边使用的。
response.setHeader(“Content-Type”,”text/html;charset=utf-8”)的快捷方法:setContentType(“text/html;charset=utf-8”).
2.请求编码
客户端发送给服务器的请求参数是什么编码
客户端首先要打开一个页面,然后在页面中提交表单或点击超链接!
在请求这个页面时,服务器响应的编码是什么,那么客户端发送请求时的编码就是什么服务器默认使用什么编码来解码参数
服务器端默认使用ISO-8859-1来解码!所以这一定会出现乱码的!因为ISO不支持中文!
请求编码处理分为两种:GET和POST,GET请求参数不在请求体中,而POST请求参数在请求体中,他们处理乱码方式是不同的。
2.3.1 GET请求编码处理
String username=request.getParameter("username"); byte[] b=username.getBytes("ISO-8859-1"); //反编码 username=new String(b,"utf-8");
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username=request.getParameter("username"); byte[] b=username.getBytes("ISO-8859-1"); //反编码 username=new String(b,"utf-8"); System.out.println("username:"+username); response.setContentType("text/html;charset=utf-8"); //响应到客户端的中文乱码 response.getWriter().print("usernmae:"+username); }
2.3.2 POST请求编码处理
request.setCharacterEncoding(“utf-8”);
String username=request.getParameter(“username”);
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //请求的中文乱码 String username=request.getParameter("username"); System.out.println("username:"+username); response.setContentType("text/html;charset=utf-8"); //响应到客户端时候解决乱码 response.getWriter().print("username:"+username);}
3.URL编码
它不是字符编码它是用来在客户端与服务器之间传递参数用的一种方式URL编码需要先指定一种字符编码,把字符串解码后,得到byte[],然后把小于0的字节+256,再转换成16进制,前面再加上%POST请求默认值使用URL编码,tomcat会自动使用URL解码URL解码:String username=URLEncoder.encode(username,"utf-8");URL编码:String username=URLDecoder.decode(username,"utf-8");
- response和request编码
- ArcIMS 9.1中REQUEST和RESPONSE编码
- servlet中response,request和编码问题
- request response 编码设置
- 笔记之--response和request对象编码等
- servlet中response和request的字符编码问题
- Request,Response编码解码原理,文件上传下载和底层IO
- Servlet中两大对象request和response的编码问题总结
- Request和Response对象
- Response和Request
- Request和Response对象
- request和response对象
- Request和Response
- response和request类
- C# Response 和 Request
- response和request
- Request和Response
- Request和Response详解
- perform_farthest_point_sampling_mesh
- Android JNI写出Hello World
- WorkFlowy格言
- nginx为什么能够替代httpd发展起来
- 使用面向对象重构之-使用接口完成行为抽象
- response和request编码
- status
- 不得不放弃、祝你元宵节快乐!
- IO流_字符流、IO流小结、IO流案例总结
- 每天一题LeetCode[第十一天]
- Go语言技巧:使用select{}阻塞main函数
- 起床闹钟
- rpm与yum
- 亚足联联赛积分规则