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.
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?
Best How To :
Yes, you can. Assuming Eclipse (your question history confirms that you're using it), just create a "Web Fragment Project":
And associate it with the main project in the wizard:
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).
It's basically a Java project with the following folder structure:
|-- com.example... (you can put e.g. @WebServlet classes here)
| |-- resources (you can put shared web resources here)
| | |-- common.css
| | |-- common.js
| | |-- template.jsp
| | :
| |-- 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 resource 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"?>
<!-- Put shared web.xml config here. -->