表单提交post方式出现乱码,get方式提交不乱码

博客提及乱码相关内容,但具体信息缺失。乱码在信息技术中常影响数据正常显示与处理。
@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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值