Ajax请求参数乱码?
2021-06-10 本文已影响0人
AIGame孑小白
应用场景:

首先我们需要打开自己的tomcat的安装位置,找到conf文件夹:

打开server.xml文件,然后找到<Service>标签,我们需要做的就是修改<Connector>标签中的内容。

只需要添加配置
useBodyEncodingForURI="true"
即可:
<Connector
connectionTimeout="20000"
port="8080"
protocol="HTTP/1.1"
redirectPort="8443"
useBodyEncodingForURI="true"/>
保存后退出,然后打开Eclipse把之前的服务器删除啦:

双击那一行小字,然后配置我们自己的tomcat进去:







注意:不要忘记Ctrl+S保存一下!!!!
最后我们测试运行一下
这里需要给请求设置一下编码参数:
@WebServlet("/ajax")
public class A extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
System.out.println("处理啦请求编码");
req.setCharacterEncoding("utf-8");
//设置请求响应编码的格式
res.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
//处理请求信息
res.getWriter().write(req.getParameter("name")+":"+req.getParameter("pass"));
}
}
启动tomcat打开浏览器访问一下:

