Servlet 3.0 web-fragment.xml
Thursday May 07, 2009
In JSR 315: Java Servlet 3.0 Specification,
web-fragment.xml is introduced for pluggability of library jars which are
packaged under WEB-INF/lib.
The content of web.xml and web-fragment.xml are almost the same.
One can define servlets, filters and listeners there. One can also specify
metadata-complete=true in a given web-fragment.xml.
In the latter case, the annotation processing of classes in that jar would be skipped.
With web-fragment.xml, library jars can be self-contained and provide web
related metadata information.
The basic differences of web.xml and
web-fragment.xml are summarized in the following table:
| web.xml | web-fragment.xml | |
|---|---|---|
| Location | WEB-INF of the war file | META-INF directory of JAR file inside WAR file's WEB-INF/lib
|
| Ordering related element | <absolute-ordering> | <ordering>
|
Ordering of web fragments
If there are more than one web-fragment jars, then one may like to specify the order of processing web-fragment.xml and annotations. This is important. For instance, filters will be executed in the order specified inweb.xml.
Similary for listeners. In Servlet 3.0, <absolute-ordering> is introduced in
web.xml and <ordering> is introduced in web-fragment.xml.
The ordering of web-fragments is specified in the following priority:
- from
<absolute-ordering>inweb.xmlif it exists - from
<ordering>for eachweb-fragment.xmlif it exists - otherwise unspecified
absolute-ordering in web.xml
The<absolute-ordering> in web.xml provides a way to specify
the ordering of loading web-fragment.xml and annotation processing
of web fragment.
For instance,
<web-app>
...
<absolute-ordering>
<name>A</name>
<others/>
<name>B</name>
<absolute-ordering>
</web-app>
In the above example, the web fragment A would be processed first
and web fragment B would be processed last.
Note the name A and B are specified in name element of
web-fragment.xml (see examples below).
ordering in web-fragment.xml
If there is no<absolute-ordering> in web.xml, then one would look at
<ordering> in web-fragment.xml. The details are described in
section 8.2.3 of Servlet 3.0 spec.
Let us look at some examples.
- There is only one jar having
<ordering>inweb-fragment.xml.
In this case, web-fragment A would be processed first.<web-fragment>
<name>A</name>
...
<ordering>
<before>
<others/>
</before>
</ordering>
</web-fragment> - There are two jars having
<ordering>inweb-fragment.xml, namely
web-fragment A:<web-fragment>
<name>A</name>
...
<ordering>
<before>
<others/>
</before>
</ordering>
</web-fragment>web-fragment B:
Both web-fragment A and B would like to be processed first. In this case, one only guarantee that both A and B are processed before other web-fragments. But the ordering of A and B are not determined, that is arbitrary in this case.<web-fragment>
<name>B</name>
...
<ordering>
<before>
<others/>
</before>
</ordering>
</web-fragment> - There are two jars having
<ordering>inweb-fragment.xml, namely
web-fragment A:<web-fragment>
<name>A</name>
...
<ordering>
<before>
<others/>
</before>
</ordering>
</web-fragment>web-fragment B:
In this case, A would be processed first, then followed by B, and then other web-fragments.<web-fragment>
<name>B</name>
...
<ordering>
<after>
<name>A</name>
</after>
<before>
<others/>
</before>
</ordering>
</web-fragment>
absolute-ordering in web.xml.











Posted by Rajiv Mordani's Blog on May 08, 2009 at 01:58 PM PDT #
Posted by Arun Gupta's Blog on May 19, 2009 at 11:01 AM PDT #
Posted by Arun Gupta's Blog on May 20, 2009 at 06:16 PM PDT #
Posted by Arun Gupta's Blog on June 23, 2009 at 06:47 PM PDT #
Posted by Arun Gupta's Blog on June 25, 2009 at 09:35 PM PDT #
Posted by Arun Gupta's Blog on July 30, 2009 at 06:01 AM PDT #
Posted by Arun Gupta's Blog on August 11, 2009 at 05:42 AM PDT #