Introduction to web fragment
This section contains elaborated introduction of Servlet 3.0 web fragment.
What is web fragment ?
The web fragment introduces in Servlet 3.0. It offers less configuration and improved pluggability to developers. A web fragment is a portion or all of the deployment descriptor(web.xml). It can be defined and enclosed in the jar file's META-INF of a framework or library. The container employs the configuration as describe in the web fragment.
A web fragment provide logical division of the web application so that the frameworks used within the web application can specify all the artifacts without requiring developers to edit or add any info in the web.xml. It employed nearly all the elements of the deployment descriptor(web.xml).
More than one web fragment can be employed. Each web fragment describe logical division. All these web fragment should be considered as complete web.xml(deployment descriptor). The web fragment provide capability to the frameworks to auto register to the web container.
Rules for utilizing web fragment
Following things should be keep in the mind before implementing web fragment :
- The web fragment must reside in a file having name web-fragment.xml.
- The top level descriptor in web-fragment.xml must be
<web-fragment>.
- The web fragment must put inside framework's JAR file's
META-INF directory. This jar file must placed inside the web
application's WEB-INF/lib directory.
- The web application's deployment descriptor have a new <metadata-complete> property . If it is sets true, annotations present in application's classes , and web fragments is ignore by the deployment tool. If it sets false or not described , annotations present in application's classes , and web fragments is examine by the deployment tool.
EXAMPLE
Given below the sample web fragment :
web-fragment.xml
<?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>webfragment1</name> <filter> <filter-name>MyFilter</filter-name> <filter-class>roseindia.MyFilter</filter-class> <init-param> <param-name>param1</param-name> <param-value>Roseindia</param-value> </init-param> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-fragment>