Filter解决中文问题

Filter类:
package com.lsh.drp.util.filter;

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 CharsetEncodingFilter implements Filter {
	
	private String encoding = "";

	public void destroy() {
		System.out.println("destroy");
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding(encoding);		
		chain.doFilter(request, response);
	}

	public void init(FilterConfig config) throws ServletException {
		encoding = config.getInitParameter("encoding");
		System.out.println(encoding);
		System.out.println("init");
	}

}


web.xml配置:
  <filter>
  	<filter-name>CharsetEncodingFilter</filter-name>
  	<filter-class>com.lsh.drp.util.filter.CharsetEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>GBK</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>CharsetEncodingFilter</filter-name>
  	<url-pattern>*.jsp</url-pattern>
  </filter-mapping>

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