zoukankan      html  css  js  c++  java
  • 最有效的解决中文乱码的方法(配置过滤器)

    1.编写过滤器的代码

    代码如下:

    package com.cx.web.filter;
    
    import java.io.IOException;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServlet;
    
    public class MyFilter extends HttpServlet implements Filter {
    
    
        public void doFilter(ServletRequest arg0, ServletResponse arg1,
                FilterChain arg2) throws IOException, ServletException {
            arg0.setCharacterEncoding("utf-8");
            arg2.doFilter(arg0, arg1);
    
        }
    
        @Override
        public void init(FilterConfig arg0) throws ServletException {
            // TODO Auto-generated method stub
    
        }
    
    }

    2.在web-xml中配置过滤器

    <filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>com.cx.web.filter.MyFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>MyFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    3.中文乱码得到解决

  • 相关阅读:
    POJ-1700 Crossing River
    C++操作符new-动态存储空间分配
    POJ-3978-Primes
    POJ-1316-Self Numbers
    浅谈二维数组在传参中的使用
    OneToMany与ManyToOne的属性
    jquery的学习
    struts2常用标签
    Struts2.xml中result type属性说明
    监听器Listener
  • 原文地址:https://www.cnblogs.com/sunnyCx/p/7760417.html
Copyright © 2011-2022 走看看