Missing elements from web-fragment.xml in the effective web.xml

java, tomcat

Solution

Strange thing now is that we have a web-fragment.xml, but some of its contents doesn't get added to the effective web.xml.

Well first of all, a `web-fragment.xml` wouldn't get physically included into the main `web.xml`. So when you say it doesn't get added to the effective `web.xml`, I feel you may be going wrong there.

Web fragments allow compoments to register themselves to the main `web.xml` at runtime. The only evidence you will find of this happening is by actually trying to use the compoment.

I tried out a simple hello-world example with web fragments and got it to work. I used Tomcat 7 for this with a Servlet 3.0 webapp.

My main `web.xml` inside `WEB-INF` looks like the below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>hello-world</display-name>

    <absolute-ordering>
        <name>filters</name>
    </absolute-ordering>
</web-app>

I figured out that the only way we could register a `web-fragment.xml` was by adhering to the below rules:

- It must be named `web-fragment.xml`

- It must be placed in a JAR file inside `WEB-INF/lib`

- The `web-fragment.xml` must be present inside the `META-INF` directory of the JAR file.

My `web-fragment.xml` looks like:

<?xml version="1.0" encoding="UTF-8"?>
<web-fragment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd" id="WebAppFragment_ID" version="3.0">
    <name>filters</name>
    <filter>
        <filter-name>interceptor</filter-name>
        <filter-class>com.adarshr.Interceptor</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>interceptor</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-fragment>

With the above minimal setup, I was able to get the `Interceptor` filter to get fired, though it was placed in the `web-fragment.xml`.

Finally, here is my directory structure (generated using http://www.adarshr.com/treegen)

+- hello-world
   |
   +- WEB-INF
       |
       +- lib
       |   |
       |   |- my-library.jar
       |
       |- web.xml

The JAR file `my-library.jar` has the below structure:

+- my-library.jar
   |
   +- META-INF
       |
       |- web-fragment.xml

Some references:

- https://blogs.oracle.com/swchan/entry/servlet_3_0_web_fragment

- http://java.sun.com/developer/technicalArticles/JavaEE/JavaEE6Overview_Part2.html

Problem

In our project we make use of web-fragments to define some servlets so the artifacts easily can be used in other projects. Strange thing now is that we have a web-fragment.xml, but some of its contents doesn't get added to the effective web.xml. By example: The following configurations is present in the effective web.xml: ``` <filter> <filter-name>superUserAutomaticLogon</filter-name> <filter-class>nl.caiw.cool.util.filters.SuperUserAutomaticLogonFilter</filter-class> <async-supported>false</async-supported> </filter> ``` But the following isn't: ``` <filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/toolbox/modules/*</url-pattern> </filter-mapping> ``` We have tried several things, but we can't figure it out. Hopefully someone here can send us in the right direction. Thanks in advance.

Original source