美文网首页
解决表单post传递乱码

解决表单post传递乱码

作者: LOOK_LOOK | 来源:发表于2017-02-11 14:45 被阅读6次

    利用filter过滤器将字符编码转变为utf-8。

    过滤器位置

    1.新建EncodingFilter.java文件

    package encoding;
    
    import java.io.*;
    import javax.servlet.*;
    
    public class EncodingFilter implements Filter {
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            try {
                request.setCharacterEncoding("utf-8");
                response.setCharacterEncoding("utf-8");
            } catch (Exception e) {
            }
    
            chain.doFilter(request, response);
        }
    
        public void destroy() {
    
        }
    }
    
    

    2.修改web.xml文件(如果没有就自己新建一个)

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        <filter>
            <display-name>encoding</display-name>
            <filter-name>encoding</filter-name>
            <filter-class>encoding.EncodingFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    </web-app>
    

    相关文章

      网友评论

          本文标题:解决表单post传递乱码

          本文链接:https://www.haomeiwen.com/subject/lkhoittx.html