Adding more and more configurations to my servlet container (Embedded Jetty in my case) I am starting to wonder whether I should put everything into a web.xml or keep the configuration programmatic in the Java code.
Are there reasons to choose web.xml over programmatic approach and vice versa? Are there security reasons or is this only cosmetic?
To me it seems web.xml is better since you dedicate a DSL to do the job instead of pressing everything into the code. On the other side I can look up configuration quickly when I am in the code.
It depends on what you want to configure and your convenience.
Servlet 3.0 allows you define the metadata in three different ways:
web.xml, Usually configuration which will change while being deployed on different environments should be configured here. These properties are such as Database properties, properties file, Admin user properties, etc.
The advantage of defining in web.xml, all the configuration exists at central place and it is easy for documentation etc.Being defined only at one place, it is difficult for maintainence during distributed development. Yes, Again Servlet 3.0 allows web-fragments.xml for which you should be defining library which can again have single descriptor per library.
Annotations, Anything which can be defined during development but can be also overridden during deployment is defined as annotations.
The advantage is No need to configure in deployment descriptor unless it has to be overridden. The annotations can give the default values.
The disadvantage is that the Container has to process all classes for annotations. I don’t see this as big disadvantage.
Programatically, Anything which you know that is not going to change can be configured programatically The advantage is that the developer is sure that the configuration is not going be altered anytime by anyone.