1

I have a Servlet Filter and due to my business logic Filter uses some variables that are initializing in when servlet's method init() invoke. So the question is: is any possibility to init filter after servlet. My Web.xml is next:

...
 <servlet>
    <servlet-name>CommonsServlet</servlet-name>
    <servlet-class>com.promptlink.dslib.gwt.common.server.rpc.CommonsServletImpl</servlet-class>
  </servlet>
...
<filter>
        <filter-name>CommonServletFilter</filter-name> 
        <filter-class>com.promptlink.dslib.gwt.common.server.httpListeners.CommonServletFilter</filter-class> 
    </filter>
    <filter-mapping>
        <filter-name>CommonServletFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <servlet-name>CommonsServletImpl</servlet-name>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
...

Maybe is any way to configure filter not in xml? I've heart that it is possible to add filter to ServletContext, but i need to add a mapping to filter too

Dante
  • 279
  • 1
  • 19

1 Answers1

1

Filters are initialized before servlets, see here for the details.

But you can create a ServletContextListener which is loaded at application startup before any filter or servlet, initialize your variables in the listener, and let your serlvets and filters then use the already initialized variables.

The listener can also add your servlets and filters programmatically, see ServletContext.addFilter() and ServletContext.addServlet().

Community
  • 1
  • 1
wero
  • 32,544
  • 3
  • 59
  • 84