10 Replies Latest reply: Mar 21, 2012 2:49 AM by 801188 RSS

    Filter for pre initialization

    801188
      Hi All,

      Is there anything wrong (From design perspective) in using a Filter for pre initialization of resources for a web Application.
      These filter instances are created by Container during application startup.
      Do we need to go for a ServletContextListener or Servlet with predefined load-on-startup for pre initialization?

      Thanks,
      Paul
        • 1. Re: Filter for pre initialization
          r035198x
          You filter requests/responses.
          Servlet is a hack because again there are no requests to serve.
          ServletContextListener is the right tool for this.
          • 2. Re: Filter for pre initialization
            gimbal2
            r035198x wrote:
            ServletContextListener is the right tool for this.
            Agreed, but do realize that the listener is invoked upon deployment; if a webapp is restarted the initialization will happen again.
            • 3. Re: Filter for pre initialization
              801188
              Thanks for your reply.

              Filter is for pre processing Request/post process response and Servelt without any mapping and load-on-startup can be used for initialization and ServletContextListener can also used for initialization .All these type of objects and created and life cycle methods are invoked by container during application deployment. But the thing is that both filter and servelt has init method and init method is invoked only once by container ,this can be used for init purpose . So for a web application having a Filter then Is it a right choice to use that filter for initialization?.Do we need to go for a ServletContextListener to do this Task?
              I have seen examples of using Servelt and ServletContextListener for initialization but not filter. Is there anything wrong in using filter for the same?
              • 4. Re: Filter for pre initialization
                EJP
                Do we need to go for a ServletContextListener to do this Task?
                Two people have already answered that question. Don't just ask it again and again.
                • 5. Re: Filter for pre initialization
                  r035198x
                  Did you read the replies?
                  798185 wrote:
                  Thanks for your reply.

                  Filter is for pre processing Request/post process response and
                  So what request/response are you processing at container startup? Using a filter for this is abuse.
                  >>Servelt without any mapping and load-on-startup can be used for initialization
                  And what request is such a servlet serving? Again more abuse.
                  .All these type of objects and created and life cycle methods are invoked by container during application deployment. But the thing is that both filter and servelt has init method and init method is invoked only once by container ,this can be used for init purpose . So for a web application having a Filter then Is it a right choice to use that filter for initialization?.
                  All these objects were created to solve different problems. Many people abuse them, don't be one of them.
                  Do we need to go for a ServletContextListener to do this Task?
                  Yes, that is what it was created for.
                  I have seen examples of using Servelt and ServletContextListener for initialization but not filter. Is there anything wrong in using filter for the same?
                  Yes. It's the wrong tool for the job.
                  • 6. Re: Filter for pre initialization
                    801188
                    Thanks All .
                     
                    Servelt without any mapping and load-on-startup can be used for initialization 
                     
                    And what request is such a servlet serving? Again more abuse. 
                    r035198x
                    Now i understood . Different Components are designed for specific purpose and we must use the correct component in disign.
                    Really sorry to bug you again. In case of web frameworks following Front Controller pattern/MVC (Struts) ,Using FC servlet for framework initialization . So in case of a servlet having servlet mapping (It will serve user req after init ,such a case can we use our servlet for application initialization) can we use that for application initialization?
                    In such a case also do we need to go for a ServletContextListener ?

                    Thanks,
                    Paul
                    • 7. Re: Filter for pre initialization
                      EJP
                      You are still asking the same question. It's been answered. However nobody can stop you using a servlet for this purpose if you are determined to do it.
                      • 8. Re: Filter for pre initialization
                        DrClap
                        But that's how they said to do it in the books published in 2002. Therefore it must be the best of all possible ways and these newfangled life-cycle listeners must be looked on with suspicion.
                        • 9. Re: Filter for pre initialization
                          gimbal2
                          DrClap wrote:
                          But that's how they said to do it in the books published in 2002. Therefore it must be the best of all possible ways and these newfangled life-cycle listeners must be looked on with suspicion.
                          Books are filled with lies, teh internets says so.
                          • 10. Re: Filter for pre initialization
                            801188
                            This makes more sense .Thanks DrClap. You got the correct context and cleared my doubt. Now it is clear to me. Thank all .