JSP表单提交中文乱码解决方法(GET方法无效)

如果是get方式,就算写Filter也不行,因为Filter针对的是post方式提交的数据,而get方式就不行了,如果用get方式必须要转码.建议用post方式。
编写一个过滤器全局编码设置:
import java.io.IOException;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;

public class EncodingFilter implements Filter {
	private String charSet;
	
	public void init(FilterConfig arg0) throws ServletException {
			this.charSet=arg0.getInitParameter("charset");
	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
			HttpServletRequest request=(HttpServletRequest)arg0;
			request.setCharacterEncoding(charSet);		//设置编码
			arg2.doFilter(arg0, arg1);
	}

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

}


WEB.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>encodingFilter</filter-name>  
	<filter-class>  
	       org.mm.filter.EncodingFilter  
	</filter-class>
	<init-param>
		<param-name>charset</param-name>
		<param-value>UTF-8</param-value>
	</init-param>
</filter>  
<filter-mapping>  
    <filter-name>  
       encodingFilter  
    </filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping> 

你可能感兴趣的:(java,jsp)