3
votes

I have different webapps that share a large portion of the web.xml configuration. For example, the way some servlets are mapped is identical for all apps, but some webapps have custom servlets, or an additional filter, or shared managed beans.

Each webapp is a different project in my IDE. I would like to refactor the common part of my web.xml to a "common" project, and let all the application-specific web.xml's extend the "common" web.xml. Is there any possibility to do this?

3
I think what you are trying to achieve can be done using annotations. Annotate a single interface with whatever mapping you need and implement that wherever you need it.shyam
https://stackguides.com/questions/25928486/tomcat-include-another-xml-file-to-web-xml This would be a good start + some Maven magic and it could do what you need.jHilscher

3 Answers

7
votes

Yes, you can. Assuming Eclipse (your question history confirms that you're using it), just create a "Web Fragment Project":

enter image description here

And associate it with the main project in the wizard:

enter image description here

You can if necessary (re)configure it in "Deployment Assembly" property of the main web project (or any other way of configuring the build in such way that it ultimately ends up as JAR in /WEB-INF/lib of main web project).

enter image description here

It's basically a Java project with the following folder structure:

CommonWebProject
 |-- com.example... (you can put e.g. @WebServlet classes here)
 |
 |-- META-INF
 |    |-- resources (you can put shared web resources here)
 |    |    |-- common.css
 |    |    |-- common.js
 |    |    |-- template.jsp
 |    |    :
 |    |
 |    |-- beans.xml
 |    |-- web-fragment.xml
 |    `-- MANIFEST.MF
 :

Any content in /META-INF/resources folder is resolved the same way as webcontent of the WAR (my Eclipse Luna SR1 didn't precreate the /resources folder, so you'd need to manually create it). And, importantingly, any resources (including classes!) with the same name already in the WAR will have loading precedence over those in JAR, so you could if necessary "override" a common JAR resource from WAR on that way.

Note that the web-fragment.xml file must be named exactly like that and thus not web.xml. The IDE should just autogenerate one for you, but for sake of completeness, a Servlet 3.0 compatible one look like this:

<?xml version="1.0" encoding="utf-8"?>
<web-fragment
    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"
    version="3.0"
>

    <!-- Put shared web.xml config here. -->

</web-fragment>

See also:

1
votes

If you deploy your apps on glassfish, than you can put your common configuration in default-web.xml file under the domain-dir\config\ location.

0
votes

Usually web servers have feature to define default web.xml which caters to your requirement. I would suggest to explore that option for the web server you are using.