This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Problem with web fragments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Problem with web fragments" Watch "Problem with web fragments" New topic

Problem with web fragments

Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183

Hi All,

I am new to this forum and need help with Servlet 3.0

I am facing a problem with web fragments .I cannot understand the ouput (am using Tomcat 7.0.23) . I
have read the web fragment part in servlet 3.0 spec "8.2.2 Ordering of web.xml and web-fragment.xml" but I think
I missed out on something.

My application contains 1 servlet , 2 filters ,web.xml and a web-fragment.xml.Following is the structure :--

There are two directories servlets and filters in WEB-INF/classes folder.
i) servlets directory contains a servlet - and its class file .
ii) filters directory contains a filter - and its class file .

There is a jar file called frag2.jar in WEB-INF/lib folder. It contains--

i) A filter and its class file in the webfragment2 folder in WEB-INF\lib\frag2.jar.
ii) A web-fragment.xml in the WEB-INF\lib\frag2.jar\META-INF directory .

The code :-

web-fragment.xml in the META-INF folder in WEB-INF\lib\frag2.jar.
---------------------------------------------------------------------------- and its class file in WEB-INF/classes/servlets folder
---------------- and its class file in WEB-INF/classes/filters folder
-------------- and its class file in the webfragment2 folder in WEB-INF\lib\frag2.jar.

Output that I am getting in Tomcat 7.0.23 is :-

Before chain.doFilter in wf1Filter
Before chain.doFilter in wf2Filter
in testservlet2
After chain.doFilter in wf2Filter
After chain.doFilter in wf1Filter

What I cannot understand is why is the wf1Filter firing first ? The web.xml has an <absolute-ordering> element which says that the
web-fragment with <name>A</name> should be executed first .So should not the wf2Filter be invoked first and then wf1Filter?
wf1Filter uses an annotation and should be processed after wf2Filter. Is not the precedence like this --> web.xml , web-fragment.xml and then
the anotations. Please help.

Please reply.


Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183


Am I supposed to post this question in the SCWCD forum. I
am not able to make progress.

Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2846

Moving this to the appropriate forum...

|Asking Good Questions|
Eshwin Sukhdeve
Ranch Hand

Joined: Mar 15, 2012
Posts: 90
Hi Simran,
you can try with this
public class wf1Filter implements Filter {}

public class wf2Filter implements Filter {}

mention in your filter classes

and configure this in your web.xml file


I dont have much idea about web-fragment.xml youi can try with..

Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183

Thanks Eshwin .My problem is web fragments.
Christian Ludt

Joined: Apr 07, 2009
Posts: 11
Hi Simran

the last paragraph in 8.2.1 Modularity of web.xml says

[...] The order in which configuration from web-fragment.xml and annotations should be applied is undefined. [...]

See the first paragraph in 8.2.3 Assembling the descriptor from web.xml, web-fragment.xml and annotations, that is even more concrete.

You do not specify the wf1Filter in a web-fragment.xml, hence the order of wf1Filter (defined by annotation) and wf2Filter (defined by web-fragment.xml) is undefined. BTW, specifying the wf1Filter in the web.xml would not lead to what you are looking for, either, as web.xml must be processed before web-fragments. The only way to achieve what you want is to provide an additional jar with META-INF/web-fragment.xml and the definition of the wf1Filter:

Probably, you have to delete the annotation in your wf1Filter class; at least Glassfish has problems in that regard, and does not overwrite the annotation, but augments it, leading to

INFO: Before chain.doFilter in wf1Filter
INFO: Before chain.doFilter in wf2Filter
INFO: Before chain.doFilter in wf1Filter
INFO: in testservlet
INFO: After chain.doFilter in wf1Filter
INFO: After chain.doFilter in wf2Filter
INFO: After chain.doFilter in wf1Filter

Best regards
Don't get me started about those stupid light bulbs.
subject: Problem with web fragments
It's not a secret anymore!