实现javax.servlet.Filter 接口 implements javax.servlet.Filter
重写里面的方法
package com.zhy.t502.utils; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class Filter implements javax.servlet.Filter { public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { arg0.setCharacterEncoding("utf-8");//UTF-8过滤器 arg2.doFilter(arg0, arg1);//放行 } public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
然后 配置 web.xml
<filter> <filter-name>utf-8</filter-name> <filter-class>com.zhy.t502.utils.Filter</filter-class> </filter> <filter-mapping> <filter-name>utf-8</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
时间: 2024-10-18 19:09:36