简体   繁体   English

弹簧过滤器称为多次

[英]Spring filter called Multiple times

I am working a spring 2.5 application that has single DispatcherServlet. 我正在工作一个具有单个DispatcherServlet的spring 2.5应用程序。 The application would also be integrated with Jersey for REST services. 该应用程序还将与Jersey集成以提供REST服务。

I am trying to filter each request to the application. 我正在尝试过滤对应用程序的每个请求。 Filter/OncePerRequestFilter (I tried both) are working fine but it seems that application is calling doFilter/doInternalFilter multiple times for single request. Filter / OncePerRequestFilter(我都尝试过)工作正常,但似乎应用程序为单个请求多次调用doFilter / doInternalFilter。 By trying three more different requests it turned out that pages in which there are more stylesheets/script files contribute to more function calls. 通过尝试另外三种不同的请求,结果发现样式表/脚本文件更多的页面有助于更多的函数调用。

Am I doing something wrong with filter standard achitecture. 我在过滤器标准结构方面做错了吗? Is there anything needed to be changed or added to application.. 是否有需要更改或添加到应用程序的任何内容。

public class UnfepiRequestFilter extends OncePerRequestFilter{

    @Override
    protected void doFilterInternal (HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("Start filtering");

        filterChain.doFilter(request, response);

        System.out.println("Done Filtering");
    }

}

web.xml file : web.xml文件:

<filter> 
    <filter-name>filterHeader</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
  </filter>
  <filter-mapping> 
    <filter-name>filterHeader</filter-name> 
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
  </filter-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/myApp-servlet.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <display-name>myApp</display-name>
  <servlet>
    <servlet-name>myApp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>myApp</servlet-name>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.action</url-pattern>
    <url-pattern>*.json</url-pattern>
  </servlet-mapping>

Basically this is to redirect or reject user requests at specific times (for maintenance tasks or temporarily making any service unavailable). 基本上,这是在特定时间重定向或拒绝用户请求(用于维护任务或暂时使任何服务不可用)。 Is there some way to prevent these multiple calls per request to the filter. 是否有某种方法可以防止每个请求对过滤器的多次调用。 Any help would be appreciated.. 任何帮助,将不胜感激..

Seems like you are not doing anything wrong. 似乎您没有做错任何事情。 Your filter is called only once per request . 每个请求仅调用一次过滤器。 However this: 但是,这:

By trying three more different requests it turned out that pages in which there are more stylesheets/script files contribute to more function calls. 通过尝试另外三种不同的请求,结果发现样式表/脚本文件更多的页面有助于更多的函数调用。

makes me think that you don't understand the way webpages are loaded by a browser. 让我认为您不了解浏览器加载网页的方式。 Each asset on a webpage (script, stylesheet, image) is loaded in its own request. 网页上的每个资产(脚本,样式表,图像)均按其自己的请求加载。 Simple HTML with no linked resources should result in single server call. 没有链接资源的简单HTML应该导致单个服务器调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM