字符过滤filter
package com.oralce.action;
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 UTF8Filter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain fchain) throws IOException, ServletException {
// TODO Auto-generated method stub
//System.out.println("先执行过滤器里面的内容。。。。。");
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
fchain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
设置web.xml内容让每次执行每一个servlet之前先执行过滤器的内容
<!-- 修改编码的UTF-8,在每个servlet执行之前进行的过滤 开始-->
<filter>
<filter-name>utf8</filter-name>
<filter-class>com.oralce.action.UTF8Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>utf8</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 修改编码的UTF-8,在每个servlet执行之前进行的过滤 结束-->
设置过滤机制简化代码,