HomeDocumentation > Reference > Samples > Java EE sample applications > webfragment-javaee6 - A simple web fragments application
{scrollbar}

Application overview

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.xml and 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.

Application content

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.

+-WEB-INF

|_ web.xml

|_ geronimo-web.xml

|_ index.html

|_ header.html

|_ Sample-docu.html

Application implementation

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.

xmlgeronimo-web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1" xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2"> <sys:environment> <sys:moduleId> <sys:groupId>org.apache.geronimo.samples</sys:groupId> <sys:artifactId>webfragment-javaee6</sys:artifactId> <sys:version>${version}</sys:version> <sys:type>car</sys:type> </sys:moduleId> <sys:dependencies/> <sys:hidden-classes/> <sys:non-overridable-classes/> </sys:environment> <context-root>/webfragment-javaee6</context-root> </web-app>

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.

xmlweb.xml ... <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>WelcomePage</servlet-name> <servlet-class>WelcomePage</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>WelcomePage</servlet-name> <url-pattern>/WelcomePage/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> <absolute-ordering> <name>fragment3</name> <name>fragment2</name> <others/> <name>fragment1</name> </absolute-ordering> </web-app>

WelcomePage.java is a servlet component which sends the request to another servlet QueryAll in fragment1.

javaWelcomePage.java ... public class WelcomePage extends HttpServlet { ... protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { ... out.println("<input type=\"submit\" name=\"QueryAll\" value=\"Query All Items\">"); ... }finally { out.close();} ...

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.

xmlweb-fragment.xml of fragment1 ... <web-fragment metadata-complete="true" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd"> <name>fragment1</name> <filter> <icon/> <filter-name>FilterFragment1</filter-name> <filter-class>org.apache.geronimo.samples.javaee6.webfragment.fragment1.FilterFragment1</filter-class> </filter> <filter-mapping> <filter-name>FilterFragment1</filter-name> <url-pattern>/messageRecord</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> <servlet> <servlet-name>QueryAll</servlet-name> <servlet-class>org.apache.geronimo.samples.javaee6.webfragment.fragment1.QueryAll</servlet-class> </servlet> <servlet-mapping> <servlet-name>QueryAll</servlet-name> <url-pattern>/QueryAll</url-pattern> </servlet-mapping> </web-fragment>

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.

mvn clean install

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.

  1. Scroll down to Deploy New from the Console Navigation panel.
  2. Load webfragment-javaee6-war-3.0-SNAPSHOT.war from webfragment-javaee6-war/target folder in to the Archive input box.
  3. 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.