JSP页面传值乱码过滤
1.过滤器类:
package com.token.t1; 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 CharacterEncodingFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("gbk"); chain.doFilter(request, response); } public void init(FilterConfig arg0) throws ServletException { } }
2.xml文件配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!--过滤器配置--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <!--此名字与<filter-mapping>下<filter-name>相同即可 --> <filter-class>com.token.t1.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern> /* </url-pattern> </filter-mapping> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
当我用<form>的默认提交方式提交时,此方法过滤不了,需要设置 method="post",才可以过滤