The general character encoding filter wording

2011-08-04  来源:本站原创  分类:Industry  人气:80 

Today, research other people's code when it suddenly found the next spring's org.springframework.web.filter package actually has a CharacterEncodingFilter.java, sweat ..... did not pay attention to it before! But that looks a bit cumbersome, so following this filter is relatively simple and practical
java code

public class CharacterEncodingFilter implements Filter {
    private String edcoding=null;
    private FilterConfig filterConfig=null;
    private boolean ignore=true;    

    public void destroy() {
         edcoding=null;
         filterConfig=null;
    }    

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain filterChain) throws IOException, ServletException {
        if(ignore==true||request.getCharacterEncoding()==null){
            String encoding=setCharacterEncoding(request);
              if(encoding!=null){
                request.setCharacterEncoding(encoding);    

                }
        }
          filterChain.doFilter(request, response);
    }    

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig=filterConfig;
        this.edcoding=filterConfig.getInitParameter("encoding");
        String value=filterConfig.getInitParameter("ignore");
        if(value==null){
            this.ignore=true;
        }else if(value.equalsIgnoreCase("true")){
            this.ignore=true;    

        }else{
            this.ignore=false;
        }    

    }
    public String setCharacterEncoding(ServletRequest request){
        return this.edcoding;
    }
}

Add the following code in web.xml

xml code

<filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
相关文章
  • The general character encoding filter wording 2011-08-04

    Today, research other people's code when it suddenly found the next spring's org.springframework.web.filter package actually has a CharacterEncodingFilter.java, sweat ..... did not pay attention to it before! But that looks a bit cumbersome, so follo

  • Depth Struts2 filter FilterDispatcher - Chinese garbled and character encoding filter 2010-06-30

    Quote A few days ago to see a post in the forum, Chinese garbled on Struts2.0, the landlord uses a spring of the character encoding filter (CharacterEncodingFilter) Uniform Code for the GBK, front to submit form data to the Action, but in Action The

  • Depth the Struts2 filter FilterDispatcher - Chinese garbage and character encoding filter (transfer) 2011-10-14

    [Size = large] quoted a few days ago to see a post in the forum, Chinese garbled on Struts2.0, the landlord uses a spring of character encoding filter (CharacterEncodingFilter) Unicode to GBK, front submit the form data to Action, but the Chinese in

  • set the character encoding filter tomcat 2011-04-19

    Open the tomcat find "server.xml" configuration file, locate the following configuration <! - A HTTP/1.1 Connector on port 8080 -> <Connector port = "8080" protocol = "HTTP/1.1" connectionTimeout = "20000"

  • Spring to provide the character encoding using the filter 2010-11-14

    <! - Spring character set filter -> <filter> <filter-name> Spring character encoding filter </ filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </ filter-class> <init-param> <p

  • Detailed java Chinese character encoding 2010-11-18

    JAVA Chinese characters have always been a headache garbled. Especially in the WEB application. Analysis of online articles and solutions are many, but always for some specific situations. Garbage problem encountered many times, after a very hard to

  • Detailed JAVA Chinese character encoding problem 2010-11-25

    Detailed JAVA Chinese character encoding problem JAVA Chinese characters have always been a headache garbled. Especially in the WEB application. Analysis of online articles and solutions are many, but always for some specific situations. Garbage prob

  • The use of AJAX to some of the issues related to sum up the character encoding 2009-07-08

    1. If the html files with meta tags set character encoding to "utf-8", the meta tag title tag must be placed on the front, otherwise it will be displayed in the IE blank, do not know the reason is a big headache because it made almost a Biao . 2

  • --- The following Unicode character encoding standard (Zhuantie) 2010-11-30

    4. Implementation Guide 4.1 encoding (Transcoding to Other Standards) General, in the Unicode encoding standard and other standards required by the mapping between the table (table) to complete, rather than the algorithm conversion. Use than a simple

  • The browser character encoding 2010-03-23

    Source: http://blog.csdn.net/yzhz Yang dispute One question: Coding problem is JAVA for beginners in the web development process is often a problem, there are a large number of online articles related to introduction, but many articles did not use th

  • Reprinted: JAVA character encoding Series 3: Java application coding issues 2010-09-06

    Another two days time to summarize / organize a bit of encoding a variety of encoding methods, and Java applications In usage here recorded for future reference. In order to constitute a complete text encoding of knowledge against and in-depth Bawo,

  • Principles of character encoding 2010-10-14

    Character coding theory keyword: basic topics coded character sets where the root of the problem with garbled paper introduces the concept of character set encoding Java character set and encoding the relationship between the content of the article f

  • Web Summary --- Servlet, JSP, filters and listeners, the scope and nine four built-in objects, EL expression language, custom tags, JSTL, an international collection of the Chinese character encoding 2011-01-01

    1, Servlet, ServletConfig: The former corresponds to a Servlet class, which corresponds to the configuration information in the web.xml (1) Servlet: init (ServletConfig): only then loaded and instantiated class is called once. service (ServletRequest

  • Java character encoding conversion 2011-07-21

    JVM The JVM starts, JVM will set up some system properties to indicate that the JVM's default locale. user.language, user.region, file.encoding and so on. You can use System.getProperties () details to view all system properties. If the English opera

  • Character Set Filter 2010-03-26

    web.xml configuration <!-- Character set filter --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>com.itmg.interseptor.PageEncodingFilter</filter-class> <init-param> <param-name>encoding&l

  • Some characters cannot be mapped using 'ISO-8859-1' character encoding 2010-04-13

    MyEclipse create a new jsp file will be saved if prompt Chinese input error: Save could not be completed. Reason: some characters cannot be mapped using "ISO-8859-1" character encoding. Either change the encoding or remove the characters which a

  • Java in ASCII.Unicode and set UTF-8 character encoding 2010-06-03

    Keywords: java code sets the first to talk about the encoding of several characters: 1. ASCII codes we know the computer inside, all the information eventually expressed as a binary string. Each binary bit (bit) has two states 0 and 1, so 8 bits can

  • Research a bit character encoding 2010-06-16

    Those who are involved in the application to the Chinese, and will encounter the character encoding problem. Not previously studied, the brain is only a rather perfunctory concept - Unicode in the interaction will not trouble the ~ specific character

  • Character encoding (Reprinted, free research) 2010-06-24

    Those who are involved in the application to the Chinese, and will encounter the character encoding problem. Not previously studied, the brain is only a rather perfunctory concept - Unicode in the interaction will not trouble the ~ specific character

  • Character encoding Xiang Jie - completely understand and grasp the coding knowledge, "garbled" no longer exist 2010-08-17

    Every programmer is inevitable encounter character encoding issues, in particular to do Web development programmers, "garbage problem" has always been a headache, maybe you have very little experience "garbled" problem, however, method