UTF-8 encoding and http parameters

前端 未结 2 838
孤城傲影
孤城傲影 2020-12-10 18:12

I am doing a simple ajax call with the YahooUI Javascript library as follows:

YAHOO.util.Connect.setForm(\'myform\');
YAHOO.util.Connect.asyncRequest(\'POST\         


        
相关标签:
2条回答
  • 2020-12-10 18:28

    Since you are using a POST query, URIEncoding="UTF-8" is not applicable here. You need to set a filter to tell Tomcat that your request encoding is UTF-8. You may use, for example, a Spring's CharacterEncodingFilter (usage, javadoc). Other implementations of such filters can also be found.

    0 讨论(0)
  • 2020-12-10 18:40

    Yes, here is one:

    package com.lfantastico.web;
    
    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;
    
    public class EncodingFilter implements Filter {
        private String encoding = "UTF-8";
    
        public void destroy() {
        }
    
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            request.setCharacterEncoding(encoding);
            response.setCharacterEncoding(encoding);
            chain.doFilter(request, response);
        }
    
        public void init(FilterConfig config) throws ServletException {
            if (config.getInitParameter("encoding") != null) {
                encoding = config.getInitParameter("encoding");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题