Servlet编码过滤器的实现

    1 编写过滤器类 encodingfilter
package test;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 charset;	public void init(filterconfig config) throws servletexception {		this.charset=config.getinitparameter("charset");	}	public void dofilter(servletrequest request, servletresponse response,			filterchain chain) throws ioexception, servletexception {		//设置统一编码		request.setcharacterencoding(this.charset);	}	public void destroy() {	}}
 
2 配置部署文件web.xml
	<filter>		<filter-name>encoding</filter-name>		<filter-class>test.encodingfilter</filter-class>		<init-param>			<param-name>charset</param-name>			<param-value>gbk</param-value>		</init-param>	</filter>	<filter-mapping>		<filter-name>encoding</filter-name>		<url-pattern>/*</url-pattern>	</filter-mapping>
  
  
 

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