servlet中关于响应到doGet和doPost响应到浏览器乱码问题

1.在这里插入图片描述
找到D:\Tomcat\apache-tomcat-8.5.35-windows-x64\apache-tomcat-8.5.35\conf中的server.xml,在里面找到connect标签,进行如下设置
在这里插入图片描述
在尾部追加URIEncoding=“utf-8”;
doPost()z中必须加上
request.setCharacterEncoding(“utf-8”);
response.setContentType(“text/html;charset=utf-8”);
才能正确响应到浏览器
doGet()中只需要加入
response.setContentType(“text/html;charset=utf-8”);即可

2.response和request的理解

request是前端浏览器将表单数据打包成一个request对象发送给Tomcat服务器,然后Tomcat服务器调用service方法或是按照表单发送方式选择调用doget还是dopost方法,然后再在方法内部处理数据
String name = request.getParameter(“name”);
String password = request.getParameter(“password”);
获取前端数据

response是Tomcat服务器将内容打包成response对象发送到前端服务器处理
PrintWriter pw = response.getWriter();
pw.println(“name:”+name+"");
pw.print(“password:”+password);
将获取到的内容在浏览器上打印

猜你喜欢

转载自blog.csdn.net/qq_41575517/article/details/88883505