filter过滤器的使用

来源:互联网 发布:为什么淘宝打不开网页 编辑:程序博客网 时间:2024/04/29 06:43

Filter接口、FilterChain 接口和FilterConfig接口

过滤器Web 服务组件,它能截获请求和响应并作处理,因此它可以在请求和响应到达目的之前向Web应用程序的请求和响应添加功能。

过滤器生活在Servlet容器中,它也有生命周期,它的生命周期由servlet容器管理。
过滤器必须是一个实现Filter接口的类的对象,否则不具备过滤器的功能。 Filter接口的主要方法:
public void init(FilterConfig fg) //被容器调用初始化过滤器
public void doFilter(ServletRequest req,ServletResponse res, FilterChain chain)// 每当有请求或响应时被容器调用进行过滤
public void destroy()//被容器调用销毁过滤器

有时一个Web应用中的过滤器不止一个,如一个过滤器完成编码转换,另一个完成验证。这时就要把它们组成链,按配置的顺序一一进行过滤,就要用到过滤器链接口FilterChain。FilterChain接口用于调用过滤器链中的一系列过滤器,通过该接口把被过滤的任务在Filter间传递,它的主要方法 :
public void doFilter(ServletRequest req,ServletResponse res)  //调用下一个过滤器,若无下一过滤器,则将请求或响应传递到目标。

过滤器应用可以使很多东西轻松实现:如权限认证,中文编码问题等。

下面以中文编码问题为例:


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 SetEncodingFilter implements Filter {
 protected String encoding = null;
 protected FilterConfig filterConfig = null;
 protected boolean ignore = true;
 public void destroy() {
  this.encoding = null;
  this.filterConfig = null;
 }
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  if (ignore || (request.getCharacterEncoding() == null)) {
   request.setCharacterEncoding(selectEncoding(request));
  }
  chain.doFilter(request, response);
 }
 public void init(FilterConfig filterConfig) throws ServletException {

  this.filterConfig = filterConfig;
  this.encoding = filterConfig.getInitParameter("encoding");
  String value = filterConfig.getInitParameter("ignore");
  if (value == null)
   this.ignore = true;
  else if (value.equalsIgnoreCase("true")
    || value.equalsIgnoreCase("yes"))
   this.ignore = true;
  else
   this.ignore = false;
 }
 protected String selectEncoding(ServletRequest request) {
  return (this.encoding);
 }
 public FilterConfig getFilterConfig() {
  return filterConfig;
 }
 public void setFilterConfig(FilterConfig filterConfig) {
  this.filterConfig = filterConfig;
 }
}

web.xml对应配置如下:


 <filter>
  <filter-name>SetCharsetEncodingFilter</filter-name>
  <filter-class>filter.SetEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>SetCharsetEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 

文章出处:http://www.diybl.com/course/3_program/java/javajs/20081118/151879.html

 

 

 

 

 

我来给你简单的说一下吧:)
首先要明确。Filter是JSP2.0里面的东西 ,顾名思义,也就是过滤器的意思
和你说说如何用吧。要用Filter就得在web.xml中配置
例:
//配置方法和配置普通的Servlet一样的哦
<filter>
      <filter-name>PrivFilter</filter-name>
      <filter-class>com.myPriv.filter.PrivFilter</filter-class>
   </filter>

<filter-mapping>
    <filter-name>PrivFilter</filter-name>
    <url-pattern>/resource/*</url-pattern>
</filter-mapping>
//com.myPriv.filter.PrivFilter是这个过滤器所在的位置。当然这个过滤器必须实现接口javax.servlet.Filter。
然后就可以在过滤器这个接口方法中增加过滤条件了
public void doFilter(ServletRequset request, ServletResponse response,FilterChain chain)
thows ServletException, IOException{

然后你可以把过滤条件写出来,写在doFilter方法里面
我这里写一个简单的例子:
public void doFilter(ServletRequest req, ServletResponse res,
   FilterChain chain) throws IOException, ServletException {
   HttpServletRequest request = (HttpServletRequest) req;
   HttpServletResponse response = (HttpServletResponse) res;
   HttpSession session=request.getSession();
   ServletContext application=session.getServletContext();
  
      if(session.getAttribute("userSession")==null)
      {
       response.sendRedirect("error/priv_error.jsp");
       return;
      }
      else
      {
       chain.doFilter(request, response);
      }

}

在上面的这个过滤方法中,如果session.getAttribute("userSession")==null)那就跳转到错误页面。如果不为空,则继续
所以
chain.doFilter(request, response);的意思就是跳转到下个页面,
明白了吧?

如果还有不明白。,可以留下你的问题

------------------------
还有忘了回答你的问题 了。java会在处理每一个Servlet时都走一次Filter的。所以你不必担心你的过滤不起作用了
而且chain.doFilter(request.response);
是必须的。没有他,程序就不会继续了:)他是让你的request和response继续运行哦
--------------------------------
这个你就可接写/*
好了。它是说对哪些范围起作里这个过滤器