@WebServlet(urlPatterns = "/Requestdemo03")
public class Requestdemo03 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* 使用post方式不乱吗,get方式就会出现乱码
* 原因:编码和解码方式不一致
* tomcat版本:8.5及以上版本
* get请求方式,request对象使用的字符集默认为utf-8
* post请求方式:request对象使用的编码方式为ISO8859-1
*
* 解决:设置request的编码方式为utf-8 requet.setCharacterEncoding("utf-8");
*
*
* tomcat版本:8.5以下版本(了解)
没有设置request的字符集
GET:??????
POST:??????
request.setCharacterEncoding("utf-8"); 只针对post方式有效
GET:??????
POST:你好
解决:
request对象默认字符集ISO8859-1
1.String类中的方法:可以把获取到的ISO8859-1编码的字符串转换为字节数组
byte[] getBytes(Charset charset) 使用指定的字符集把字符串转换为字节数组
2.String类的构造方法:把字节输出以UTF-8的方式解码为字符串
String(byte[] bytes, String charsetName) 把字节数组,根据字符集转换字符串
*
* */
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username+"----"+password);
}
}
表单提交post方式出现乱码,get方式提交不乱码
最新推荐文章于 2023-02-17 13:07:01 发布
博客提及乱码相关内容,但具体信息缺失。乱码在信息技术中常影响数据正常显示与处理。
6255

被折叠的 条评论
为什么被折叠?



