Search code examples
javaeclipsemavenweb.xml

Master web.xml to use in all webapp projects


I'm developing multiple web applications in Eclipse Mars 2 using java 1.7. I build the files using Maven and test them using a jboss (WildFly) plugin. Each of them uses a web.xml and share 90% of their logic. They all use spring, they all use the same session config, the same filters etc. The difference is the authorization checks and security roles.

Recently I had to do some updates, and it was burden to update all web.xml separately. I'm looking for a solution to define a "web.xml" parent or master file which houses all the common logic, and then inject the small specific parts. What options do I have?


Solution

  • I would move as much as you can out of web.xml and into annotations on the classes. For example, on a filter, you can do something like:

    import javax.servlet.Filter;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.annotation.WebInitParam;
    
    @WebFilter(filterName = "TimeOfDayFilter",
    urlPatterns = {"/*"},
    initParams = {
        @WebInitParam(name = "mood", value = "awake")})
    public class TimeOfDayFilter implements Filter {
    

    (taken from here)

    In this way, you've moved much of the traditional configuration that is done in web.xml into the Java class. The same thing can be done with servlets.

    Note that this isn't always the right answer. In filters, for example, you can only order them (that is, have a chain of filters) if you're using web.xml. But the more you can move out of web.xml the better.