• 使用过滤器解决JSP页面的乱码问题


    乱码详情
     总结:讨论了使用GET和POST方法,控制台和JSP页面显示的问题。
    最终发现:在servlet或者过滤器中添加:request.setCharacterEncoding("utf-8");
            res.setContentType("text/html;charset=utf-8");
     即可解决页面显示乱码问题。
     
    JSP页面中
    <%@ page language="java"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form action="${pageContext.request.contextPath}/encodingServlet" method="get">  //选择GET或者POST提交
     
    <input type="text" name="text" > <input type="submit"> </form> </body> </html>
     
     
    过滤器页面:  
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                       throws IOException, ServletException {
                 HttpServletRequest req = (HttpServletRequest) request;
                 HttpServletResponse res = (HttpServletResponse) response;
          
                 //放行!!!——————————————————————
                 chain.doFilter(newRequest, res);
                 
          }
    编写的servlet的get方法中:
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          String str=request.getParameter("text");
          if(str!=null)System.out.println(str);
          response.getWriter().write(str);
          }
     
     
    我们在过滤器中注释掉

    //req.setCharacterEncoding("utf-8");
    // res.setContentType("text/html;charset=utf-8");

     
    控制台显示
    JSP页面显示
    GET
    你好啊 
    ???
    POST
    你好å 
    :浣犲ソ鍟�
     
    在filte过滤器加上:request.setCharacterEncoding("utf-8");
            res.setContentType("text/html;charset=utf-8");
     
     
     
    控制台显示
    JSP页面显示
    GET
    你好啊 
    你好啊
    POST
    你好啊 
    你好啊
     
     
    这里JSP页面出现乱码是因为:在getParameter()方法中,得到的字符串是乱码的。
     
    在Filter中:
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
    
            HttpServletRequest req = (HttpServletRequest) request;
            HttpServletResponse res = (HttpServletResponse) response;
        
            //放行!!!——————————————————————
    //        
            EnhanceRequest newRequest=new EnhanceRequest(req);
        //      req.setCharacterEncoding("utf-8");
        //    res.setContentType("text/html;charset=utf-8");
            System.out.println("filtering");
            chain.doFilter(newRequest, res);
            
        }
    使用装饰者模式,增强getParameter().
    小插曲:  装饰者模式:
    增强类与被增强类实现统一接口。
    增强类中传入被增强的类
    需要增强的方法重写。
    之后在Filter中增强req:
    class EnhanceRequest extends HttpServletRequestWrapper {
          private HttpServletRequest request;
          public EnhanceRequest(HttpServletRequest req) {
                 super(req);
                 this.request=req;
          }
          //对getParameter增强
          @Override
          public String getParameter(String name) {
                 String parameter =request.getParameter(name);//乱码
                 try {
                       parameter=new String(parameter.getBytes("iso8859-1"),"UTF-8");
                 } catch (UnsupportedEncodingException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                 }
                 return parameter;
          }
          
    }    
     
    控制台显示
    JSP页面显示
    GET
    ???
    ???
    POST
    你好啊
    ???
     同时发现:当JSP页面中去除掉这条代码contentType="text/html; charset=UTF-8",将造成JSP页面显示数据为乱码。
    结论:要解决JSP页面和控制台的乱码问题,只要在filter过滤器加上:request.setCharacterEncoding("utf-8");
            res.setContentType("text/html;charset=utf-8");
    或者就在servlet页面上添加:request.setCharacterEncoding("utf-8");
            res.setContentType("text/html;charset=utf-8");
     便可解决POST和GET方法的乱码问题。
     
     
     
     
     
  • 相关阅读:
    Linux系统常用工具集
    Storm安装部署
    Linux下搭建Elasticsearch7.6.2集群
    解决SpringMVC @RequestBody无法注入基本数据类型
    微服务概念
    HashMap的原理简单介绍
    mysql进阶
    vue 路由缓存 keep-alive include和exclude无效
    el-date-picker 时间日期格式,选择范围限制
    RedisTemplate使用rightPushAll往list中添加时的注意事项
  • 原文地址:https://www.cnblogs.com/patatoforsyj/p/9983046.html
Copyright © 2020-2023  润新知