× Home MicroServices WYSI!WYG Zen IDE AppServer JSP JDBC Pool EJB Filter Bug About Services
Backbutton Frameworks ©

Filter Bug

The following code produced a bug.

               // Get init Parameter
       		    String testParam = config.getInitParameter("example-param");
I noticed that as soon as I completed the coding including setting up the filter params in web.xml, both the init and doFilter methods was running twice. The init() running twice was a special concern. in the first run variable testParam picked up the correct value , but on the second run it produced a null.

The way I have my set up I do the code one step at a time then run the clean package wildfly:undeploy wildfly:deploy to see the results. Anyway I first did a google search for FilterConfig running twice. StackOverflow show that somebody had already had the same problem. There were about 10-12 answers.

I took out and put back the Filter config TAGS init param from the web.xml, each time doing a compile ,deploy,run , test. This way I identified the cause of problem quite quickly. I then looked for annotation syntax if there was a way to set the init param that way. As it happens there is :

             initParams = {@WebInitParam(name = "mood", value = "awake")})
putting this one line of code in the java class rather than populating the web.xml the outcome was as expected. The init() and doFilter() methods ran only once , with Dofilter() running after that each time the overlaying servlet called and init() not running again after the first time. I can replicate this problem therefore it is bug. This bug, although implementation bug is not limited to wildfly , other application server(s) according to StackOverFlow are also exhibiting the same incorrect behaviour. I am rather surprised that such an obvious bug has not been identified and or fixed.

The google search for the bug also produced another result page with same question and answer. On that page it was suggested to set a boolean flag=false and after first execution set to true with an IF statement preventing second execution. The test result and it took a few minutes to realise until I felt rather stupid because the boolean flag=false was doing it twice on every occasion as the whole class and every method was being run twice including the init() method.

It is my opinion that using annotation is better for code readability simply because you can see the init params key value pair while using the web.xml means one has to unnecessarily refer to another file.