servlet过滤器使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ouyanggengcheng/article/details/77775929

在使用Struts的时候,发现请求过来的数据总是乱码,通过排查,在ActionServlet实例化ActionForm使用set方法的时候已经是乱码,而请求第一步都会经过web服务器,于是需要在web服务器配置一个过滤器来把请求数据转化。


1.过滤器代码:

很简单,就把内容都先转化成utf-8

public class FilterCharset extends HttpServlet implements Filter {


@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
// TODO Auto-generated method stub
arg0.setCharacterEncoding("utf-8");
arg2.doFilter(arg0, arg1);

}


@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}


}



2.web.xml配置

<filter>
  <filter-name>FilterCharset</filter-name>
  <filter-class>com.struts.filter.FilterCharset</filter-class>
  </filter>
  
  <filter-mapping>
  <filter-name>FilterCharset</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>


/*代表所有的内容都转化。

猜你喜欢

转载自blog.csdn.net/ouyanggengcheng/article/details/77775929
今日推荐