zoukankan      html  css  js  c++  java
  • Servlet------>request和response控制编码乱码问题

    我在request篇和response都有提到,觉得会忘记,所以从新整理一下

    request细节四----->通过request控制编码问题

    第一种方式是通过设置------>request.setCharacterEncoding("UTF-8")和URLEncoder.encode(username, "UTF-8");//只有post生效

    第二种方式是通过设置------>(post,get通用的情况)

    String username=new String(request.getParameter("username").getBytes("iso8859-1"),"UTF-8");//反向查找,get/post都可以

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //        request.setCharacterEncoding("UTF-8");//只有post生效
            String username=new String(request.getParameter("username").getBytes("iso8859-1"),"UTF-8");
            //反向查找,get/post都可以
            //URLEncoder.encode(username, "UTF-8");
            System.out.println(username);
        }
    

     在浏览器头设置好如下

    <meta charset="UTF-8">
    

     图片是原理:

    第三种方式是通过设置------>在uri里带参数的情况,可以在tomcat server.xml里配置

    第四种方式是通过设置------>(post,get通用的情况)

    首先servlet里配置:

    然后:server.xml里配置:

    以上是request编码解决办法,然后来讲下response中乱码解决:

     HttpServletResponse细节一-------》码表的对应设置

    原理图:

    浏览器读出涓球理由:浏览器默认国标码表读,不是utf-8

    字节流解决办法--->控制浏览器查找UTF-8码表

     private void test2(HttpServletResponse response) throws IOException, UnsupportedEncodingException {
            //用html技术中的meta标签模拟http响应头,来控制浏览器的行为
            String data="中国";
            OutputStream out=response.getOutputStream();
             
            out.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>".getBytes());
            out.write(data.getBytes("UTF-8"));
        }
     
        private void test1(HttpServletResponse response) throws IOException, UnsupportedEncodingException {
            //以什么编码发就用什么编码读
            response.setHeader("Content-type","text/html;charset=UTF-8");
            String data="中国";
            OutputStream out=response.getOutputStream();
            out.write(data.getBytes("UTF-8"));
        }
    

    字符流发生问题:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         
        String data="中国";
        PrintWriter out=response.getWriter();
        out.write(data);
    }
    

     先把中国写入respone,因为respone是外国人发明的,查找的是iso8859这个码表,查找不到所以显示??

     

    字符流解决办法--->改变所查码表和浏览器所读取时查询码表

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //        response.setCharacterEncoding("UTF-8");//控制写入response时所查询的码表
    //        response.setHeader("content-type", "text/html;charset=UTF-8");//控制浏览器输出时时所查询的码表
            response.setContentType("text/html;charset=UTF-8");//这句话可以代替上两句话
            String data="中国";
            PrintWriter out=response.getWriter();
            out.write(data);//writer流只能写字符串!
        }
    

    成功:

  • 相关阅读:
    Mac 实用工具——迁移助理
    Mac OS 的终端工具 iTerm2 的使用及设置
    Python3的异常捕获和处理
    Python3 文件的重命名
    Linux下jetty的启动和停止
    MySQL使用select查询时,在查询结果中增加一个字段并指定固定值
    使用ThreadLocal请务必remove
    Vue基础-文本显示,v-html插入html代码
    nginx之location(root/alias)&& linux 上修改了nginx.conf 怎么重新加载配置文件生效
    CentOS7开启防火墙及特定端口
  • 原文地址:https://www.cnblogs.com/SnowingYXY/p/6756439.html
Copyright © 2011-2022 走看看