javax.servlet.Filter类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(247)

本文整理了Java中javax.servlet.Filter类的一些代码示例,展示了Filter类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Filter类的具体详情如下:
包路径:javax.servlet.Filter
类名称:Filter

Filter介绍

[英]A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.

Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, and a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks.

Filters are configured in the deployment descriptor of a web application.

Examples that have been identified for this design are:

  1. Authentication Filters
  2. Logging and Auditing Filters
  3. Image conversion Filters
  4. Data compression Filters
  5. Encryption Filters
  6. Tokenizing Filters
  7. Filters that trigger resource access events
  8. XSL/T filters
  9. Mime-type chain Filter
    [中]过滤器是一个对象,它对资源请求(servlet或静态内容)或资源响应执行过滤任务,或同时对两者执行过滤任务。
    过滤器在doFilter方法中执行过滤。每个筛选器都可以访问一个FilterConfig对象,从中可以获取其初始化参数,还可以访问对ServletContext的引用,例如,它可以使用该引用加载筛选任务所需的资源。
    过滤器在web应用程序的部署描述符中配置。
    本设计已确定的示例包括:
    1.身份验证筛选器
    1.日志记录和审核过滤器
    1.图像转换滤波器
    1.数据压缩过滤器
    1.加密过滤器
    1.标记化过滤器
    1.触发资源访问事件的筛选器
    1.XSL/T过滤器
    1.Mime类型链过滤器

代码示例

代码示例来源:origin: jenkinsci/jenkins

public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    if(position==filters.length) {
      // reached to the end
      chain.doFilter(request,response);
    } else {
      // call next
      filters[position++].doFilter(request,response,this);
    }
  }
}.doFilter(request,response);

代码示例来源:origin: spring-projects/spring-framework

/**
 * Initialize all the filters, calling each one's init method in turn in the order supplied.
 * @see Filter#init(FilterConfig)
 */
@Override
public void init(FilterConfig config) throws ServletException {
  for (Filter filter : this.filters) {
    filter.init(config);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void destroy() {
  this.delegate.destroy();
}

代码示例来源:origin: org.jasig.portal/uPortal-spring

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
  if (wrappedFilter == null) {
    throw new ServletException("No wrappedFilter configured");
  }
  if (this.enable) {
    this.wrappedFilter.doFilter(request, response, chain);
  } else {
    chain.doFilter(request, response);
  }
}

代码示例来源:origin: com.google.inject.extensions/guice-servlet

throw new ServletException(
   "Filters must be bound as singletons. "
     + filterKey
filter.init(
  new FilterConfig() {
   @Override

代码示例来源:origin: sitemesh/sitemesh3

protected void deployNewFilter(Filter newFilter) throws ServletException {
  Filter oldFilter = filter;
  if (newFilter == null) {
    throw new ServletException("Cannot deploy null filter");
  }
  newFilter.init(filterConfig);
  filter = newFilter;
  if (oldFilter != null) {
    oldFilter.destroy();
  }
}

代码示例来源:origin: Atmosphere/atmosphere

try {
    filter = filterConfig.getFilter();
    filter.doFilter(request, response, this);
  } catch (IOException e) {
    throw e;
    throw e;
  } catch (Throwable e) {
    throw new ServletException("Throwable", e);
    servlet.service(request, response);
  } else {
    RequestDispatcher rd = configImpl.getServletContext().getNamedDispatcher("default");
    if (rd == null) {
      throw new ServletException("No Servlet Found");
  throw e;
} catch (Throwable e) {
  throw new ServletException("Throwable", e);

代码示例来源:origin: sitemesh/sitemesh3

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  if (!initialized) {
    throw new ServletException(getClass().getName() + ".init(FilterConfig) was not called");
  }
  reloadIfNecessary();
  filter.doFilter(servletRequest, servletResponse, filterChain);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Pass the call on to the Filter/Servlet.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws ServletException, IOException {
  if (this.filter != null) {
    this.filter.doFilter(request, response, this.nextFilterChain);
  }
  else {
    Assert.state(this.servlet != null, "Neither a Filter not a Servlet set");
    this.servlet.service(request, response);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Actually invoke the delegate Filter with the given request and response.
 * @param delegate the delegate Filter
 * @param request the current HTTP request
 * @param response the current HTTP response
 * @param filterChain the current FilterChain
 * @throws ServletException if thrown by the Filter
 * @throws IOException if thrown by the Filter
 */
protected void invokeDelegate(
    Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
    throws ServletException, IOException {
  delegate.doFilter(request, response, filterChain);
}

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
public void testFilterBasic() throws ServletException, IOException {
  Filter filter = makeFilter();
  try {
    filter.init(makeFilterConfig());
    filter.doFilter(makeRequest(), makeResponse(), makeFilterChain());
  }
  finally {
    filter.destroy();
  }
}

代码示例来源:origin: nutzam/nutz

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
  if (proxy == null) {
    synchronized (lock) {
      if (proxy == null) {
        Ioc ioc = Mvcs.ctx().getDefaultIoc();
        Filter proxy = ioc.get(null, beanName);
        proxy.init(filterConfig);
        this.proxy = proxy;
      }
    }
  }
  proxy.doFilter(request, response, chain);
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Reset the proxies and filter for a change in {@link SecurityRealm}.
 */
public void reset(SecurityRealm securityRealm) throws ServletException {
  if (securityRealm != null) {
    SecurityRealm.SecurityComponents sc = securityRealm.getSecurityComponents();
    AUTHENTICATION_MANAGER.setDelegate(sc.manager);
    USER_DETAILS_SERVICE_PROXY.setDelegate(sc.userDetails);
    REMEMBER_ME_SERVICES_PROXY.setDelegate(sc.rememberMe);
    // make sure this.filter is always a valid filter.
    Filter oldf = this.filter;
    Filter newf = securityRealm.createFilter(this.filterConfig);
    newf.init(this.filterConfig);
    this.filter = newf;
    if(oldf!=null)
      oldf.destroy();
  } else {
    // no security related filter needed.
    AUTHENTICATION_MANAGER.setDelegate(null);
    USER_DETAILS_SERVICE_PROXY.setDelegate(null);
    REMEMBER_ME_SERVICES_PROXY.setDelegate(null);
    filter = null;
  }
}

代码示例来源:origin: Atmosphere/atmosphere

/**
 * Initialize the {@link Filter}
 *
 * @throws javax.servlet.ServletException
 */
public void init() throws ServletException {
  for (FilterConfigImpl f : filters) {
    if (f != null) {
      f.getFilter().init(f);
    }
  }
  if (servlet != null) {
    servlet.init(configImpl);
  }
}

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
public void testInit() {
  Filter filter = makeFilter();
  try {
    filter.init(makeFilterConfig());
  }
  catch (ServletException e) {
    assertNotNull(e.getMessage());
  }
  finally {
    filter.destroy();
  }
}

代码示例来源:origin: igniterealtime/Openfire

servlet.destroy();
filter.destroy();

代码示例来源:origin: org.jasig.portal/uPortal-spring

@Override
public void init(FilterConfig filterConfig) throws ServletException {
  if (wrappedFilter == null) {
    throw new ServletException("No wrappedFilter configured");
  }
  if (this.enable) {
    this.wrappedFilter.init(filterConfig);
  }
}

代码示例来源:origin: org.sitemesh/sitemesh

protected void deployNewFilter(Filter newFilter) throws ServletException {
  Filter oldFilter = filter;
  if (newFilter == null) {
    throw new ServletException("Cannot deploy null filter");
  }
  newFilter.init(filterConfig);
  filter = newFilter;
  if (oldFilter != null) {
    oldFilter.destroy();
  }
}

代码示例来源:origin: org.sitemesh/sitemesh

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  if (!initialized) {
    throw new ServletException(getClass().getName() + ".init(FilterConfig) was not called");
  }
  reloadIfNecessary();
  filter.doFilter(servletRequest, servletResponse, filterChain);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Pass the call on to the Filter/Servlet.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws ServletException, IOException {
  if (this.filter != null) {
    this.filter.doFilter(request, response, this.nextFilterChain);
  }
  else {
    Assert.state(this.servlet != null, "Neither a Filter not a Servlet set");
    this.servlet.service(request, response);
  }
}

相关文章

微信公众号

最新文章

更多