利用过滤器解决jsp中的乱码问题

package cn.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;
import javax.servlet.http.HttpServletRequest;


public class FilterDemo implements Filter {
private FilterConfig filterConfig;


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

}


@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String encoding = filterConfig.getInitParameter("encoding");
if(encoding==null){
encoding = "utf-8";
}
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
response.setContentType("text/html;charset = "+encoding);

chain.doFilter(request, response);

}


@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;

}


}


在jsp中配置flter

 
 
  FilterDemo
  cn.filter.FilterDemo
 
  encoding
  utf-8
 

 

 
  FilterDemo
  /*
 

 


你可能感兴趣的:(利用过滤器解决jsp中的乱码问题)