Geertjan is a DZone Zone Leader and has posted 457 posts at DZone. You can read more from them at their website. View Full User Profile

web-fragment.xml: I Don't Get It

08.17.2010
| 10144 views |
  • submit to reddit

I've been trying to understand web-fragment.xml, which is part of the Servlet 3.0 specification. The confusion comes from the discussion in this issue in NetBeans Issuezilla:

http://netbeans.org/bugzilla/show_bug.cgi?id=187548

There it is posited that a web-fragment.xml file makes sense "in a project in which a web framework or a support web library is being developed". Similarly, in Arun Gupta's introduction to web-fragment.xml in a Maven-Wicket application (here), he ends by saying: "It would be nice if the next version of wicket-*.jar can include “META-INF/web-fragment.xml” then everything will work out-of-the-box."

In other words, the idea appears to be that the provider of a web framework library (e.g., the provider of Wicket, the provider of Struts, the provider of JSF), will be providing a web-fragment.xml file inside the Wicket/Struts/JSF library.

However, let's take a look at the content of a web-fragment.xml file obtained from Arun's blog entry, below. Here you can see that the application class name is defined within the web-fragment.xml file:

 

However, isn't the application class name defined per application, rather than per web framework library? To use the web-fragment.xml file that Arun provided, I needed to change the package naming and class name of the Wicket application class in my own application. That can't be right.

I imagine that the web-fragment.xml file should, somehow, be defined per application. So, if NetBeans IDE only lets me create such a file within a Java SE library project, doesn't that mean that I should be defining my entire Wicket application within a Java SE project? Then one would be modularizing the web.xml file, with each fragment being provided by a different JAR, with each JAR contributing a framework-specific part of the application. But that, according to the NetBeans Issue above, is not the idea at all. 

Though I feel that the web-fragment.xml file should be defined within a web application, it appears that only JAR files packaged in WEB-INF/lib are scanned for META-INF/web-fragment.xml, i.e., WAR files are not searched for this file.

So either I don't get it or there's something odd going on. Probably I simply don't get it. Is there a real scenario anywhere where web-fragment.xml is demonstrated, i.e., where there is more than a single web-fragment.xml? (Though a real sample with even one single web-fragment.xml would be interesting to see.) Especially, where do web frameworks (e.g., Wicket, Struts, JSF) fit into the web-fragment world? (From this, it would appear that if I want to use a Wicket filter in a web-fragment.xml file, the whole Wicket application would be created in a JAR file, which means that Java SE Library projects in NetBeans IDE should let me add support for web frameworks to them.) I haven't found a relevant sample thus far.

 

Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Simon Peter replied on Tue, 2010/08/17 - 8:16am

web-fragments.xml are pre-defined pieces that combined will form the actual web.xml that makes the web application. Because (some) frameworks always require the same pieces of code in the web.xml, they now can do that for you. This way, they can provide their library .jar files, without you to also add some configuration (you can still override this though).

Since a web project (WAR) already requires the use of the web.xml file, it makes absolutely no sense in creating web-fragments.xml. You simply can/must add them to the web.xml. It also does not make sense to add a WAR inside theWEB-INF/lib of another WAR. Make it a JAR and you can use the web-fragments or otherwise use it as a separate web-application.

The final web application is made up from the web-fragments, tagged servlet classes on the classpath and the web.xml, where the web.xml can override the others. You can compare the web-fragments as pieces of the web.xml, but just declared somewhere else.

Making your (wicket) web application modular by adding jars with web-fragments is exactly what the idea behind this seems to be. The jars can be a fragment of the application, allowing you to reuse them in other applications. Adding more would then enhance your application.

As for you example: this is also an example, just showing you how you can do it. The guys from Wicket will not use it that way. Since each application has it's own classpath, it can work. But when the class is location in a shared module, this will most definetly result in problems.

Julio Aguilar replied on Tue, 2010/08/17 - 12:17pm

My two cents: 

 

To use the web-fragment.xml file that Arun provided, I needed to change the package naming and class name of the Wicket application class in my own application. That can't be right.

Well, I'd think the problem here would be that the WicketFilter takes the classname of the Application as a parameter. Other frameworks don't have that requirement (although some have similar needs like a base package name or a configuration file location). I imagine Wicket (and other frameworks) should change the way of finding that information to be able to use web-fragment.xml in the more effective way.

 

which means that Java SE Library projects in NetBeans IDE should let me add support for web frameworks to them.

 As they do in NB 6.9.1 (Not sure about 6.9)

 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.