This sample demonstrates the modularity and plug-ability features in Servlet 3.0 specification, which is also call as web fragments. A web fragment is like a part of a web application, which consumes almost all the same elements as a web application within its deployment descriptor, however, the deployment descriptor of a web fragment must be named as
web-fragment.xml and put in
META-INF/ directory of the fragment. And the top-level element must be <web-fragment>. You can update a web fragment without any impact to the web application.
Within the web application, all configuration files such as
web-fragment.xml are loaded and scanned. You can specify the ordering requirements of the web application use either <absolute-ordering> or <order> element. This sample only shows the usage of <absolute-ordering> element.
The sample application consists of following list of packages and classes.
org.apache.geronimo.samples.javaee6.webfragment.fragment1 is the first fragment of the web application
- FilterFragment1.java is a filter classes in fragment1 to the request to /messageRecord.
- QueryAll.java displays all items you can buy.
- META-INF/web-fragment.xml is the deployment descriptor of the fragment1.
org.apache.geronimo.samples.javaee6.webfragment.fragment2 is the second fragment of the web application
- FilterFragment2.java is a filter classes in fragment2 to the request to /messageRecord.
- BuyRecordItem.java perform the actual buy action.
- ShoppingCart.java puts the items you bought into a shopping cart.
- META-INF/web-fragment.xml is the deployment descriptor of the fragment2.
org.apache.geronimo.samples.javaee6.webfragment.fragment3 is the first fragment of the web application
- FilterFragment3.java is a filter classes in fragment3 to the request to /messageRecord.
- Payment.java calculate how much you should pay.
- ServletListener.java is a listener class in fragment3 to all the request to /Payment servlet.
- META-INF/web-fragment.xml is the deployment descriptor of the fragment3.
org.apache.geronimo.samples.javaee6.webfragment is the main body of the web application
- WelcomePage.java shows the welcome page and the brief introduction of the sample application.
- messageRecord.java displays all the filter messages and listener messages during the purchase.
The list of web application files in the webfragment application is depicted in the following.
geronimo-web.xml specifies the module's information and the url for the web application
Information about the project sucha as module's unique identification, dependencies is described inside the <sys:environment/> tags. It is a good practise to give a module an unique identification, so that it can later be referenced by some other deployable application. This module is in the group org.apache.geronimo.samples. The path specified in the <context-root> tag will be the entry point of this web application. Therefore you can access this web application at http://<hostname>:<port>/webfragment-javaee6.
web.xml defines the welcome page of the webfragment application and the order of resources to be composed from fragments. Using <absolute-ordering> means that web.xml must be processed first, fragment3 and fragment2 must be processed sequentially. <others/> element makes sure the other web fragments being processed before fragment1. And fragment1 is the last one to be processed.
WelcomePage.java is a servlet component which sends the request to another servlet QueryAll in fragment1.
web-fragment.xml uses the same elements as in web.xml except that the top-level element must be <web-fragment>. And metadata-complete="true" means any annotatons in the class files within this fragment will not be processed. <name>fragment1</name> must be consistent with the one in web.xml of webfragment application.
You can look into other web framents, and you will notice the similar configuration information and some other servlets which are pretty easy to understand.
Get the source code
Please reference Samples General Information for information on obtaining and building the source for this and other samples.
Build the web application
Once all the sources get checked out the next step is to build webfragment-javaee6. It requires Maven 2 for building the binaries.
From the <webfragment-javaee6_home> directory run the following command.
This process will take a couple of minutes. The binaries will be generated in the corresponding target directory .
Deploy the web application
Deploying sample application is pretty straight forward as we are going to use the Geronimo Console.
- Scroll down to Deploy New from the Console Navigation panel.
- Load webfragment-javaee6-war-3.0-SNAPSHOT.war from webfragment-javaee6-war/target folder in to the Archive input box.
- Press Install button to deploy application in the server.
Test the web application
The app is visible at http://localhost:8080/webfragment-javaee6/
Input the ID and quantity you want to buy, then click Add to cart
Click Go to Pay to see how much you should pay and all messages that the filter or listener classes recorded.