简体   繁体   中英

Spring filter called Multiple times

I am working a spring 2.5 application that has single DispatcherServlet. The application would also be integrated with Jersey for REST services.

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. 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 :

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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