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:
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: