javaWeb里的请求和响应乱码问题解决HttpServletRequest,HttpServletResponse

doPost

@WebServlet("/servlet2")//使用此注解,就不用在web.xml里配置Servlet了
public class Servlet2 extends HttpServlet {
    
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        //设置请求体的字符集位utf-8,从而解决post请求的中文乱码问题
        req.setCharacterEncoding("utf-8");//解决post请求乱码
        resp.setContentType("text/html;charset=UTF-8");
        String username = req.getParameter("username");
        System.out.println(username);
        resp.getWriter().write("你好,我是尚硅谷");
    }
}

doGet

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_60664694/article/details/128290702