乱码问题如下:
提交参数后:
- post方式乱码:
- get方式:没有乱码
解决方法:
在获取参数前,设置request的编码:
request.setCharacterEncoding("utf-8");
例如:
@WebServlet("/requestbody3")
public class requestDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置流的编码
request.setCharacterEncoding("utf-8");
//获取请求参数
String username = request.getParameter("username");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);//执行doPost代码逻辑,简化代码
}
}