栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HttpServlet中文乱码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HttpServlet中文乱码

设置相同字符集UFT-8    TomCat8.0默认设置

1.POST中文乱码
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/req4")
public class RequestDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.解决乱码:POST,getReader()
        request.setCharacterEncoding("UTF-8");//设置字符输入流的编码

        //2.获取username
        String username = request.getParameter("username");
        System.out.println(username);

        
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

 HTML设置




    
    Title





游泳 爬山
2.GET中文乱码

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/req4")
public class RequestDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //2.获取username
        String username = request.getParameter("username");
        System.out.println("解决乱码前:" + username);

        //3.GET,获取参数的方式:getQueryString
        // 乱码原因:tomcat进行URL解码,默认的字符集是ISO-8859-1   8.0之后默认是UTF-8
        //3.1 先对乱码数据进行编码:转换字节数组
//        byte[] bytes = username.getBytes("ISO-8859-1");
//        //3.2字节数组解码
//        username = new String(bytes,"UTF-8");

        username = new String(username.getBytes("ISO-8859-1"),"utf-8");

        System.out.println("解决乱码后" + username);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

 更容易理解:

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class URLDemo {

    public static void main(String[] args) throws UnsupportedEncodingException {

        String username = "张三";

        //1.URL编码
        String encode = URLEncoder.encode(username, "utf-8");
        System.out.println(encode);

        //2.URL解码
        String decode = URLDecoder.decode(encode, "ISO-8859-1");
        System.out.println(decode);

        //3.转换为字节数据,编码
        byte[] bytes = decode.getBytes("ISO-8859-1");
//        for (byte b : bytes){
//            System.out.print(b + " ");
//        }

        //4.将字节数组转换为字符串,解码
        String s = new String(bytes, "utf-8");
        System.out.println(s);

    }
}

输出结果:

 总结:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/981593.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号