888
888

Reputation: 3416

Set web.xml configuration programmatically

Since Servlet 3.0 were introduced the annotations:

So is it possible to completely remove the web.xml and set all cofigurations contained in it programmatically? If this is true, is it a good practice or should it be avoided?

edit: In the link provided by farrellmr is used org.springframework.web.WebApplicationInitializer;, but I'm looking for a general approach without the use of a particular framework.

Upvotes: 1

Views: 1330

Answers (1)

Miljen Mikic
Miljen Mikic

Reputation: 15261

Q: So is it possible to completely remove the web.xml and set all cofigurations contained in it programmatically?

Yes, starting with Servlet 3.0, you can have a Web application without web.xml file.

Q: If this is true, is it a good practice or should it be avoided?

It depends. A drawback of having web.xml in a large Web application with many servlets is that this file becomes larger and larger, to the point that is hard to maintain. On the other hand, you may have a specific role in your team, a deployer, who does not need to know how to program. With web.xml, he can easily change e.g. URL of the servlet by changing the content of the file and simply redeploying the application. Finally, be aware that Servlet 3.0 is supported only by Tomcat7+ and other, "newer", application servers / servlet containers. Our team once had a client with Tomcat6 and they complained about some strange errors, which you can guess, were caused by using annotations instead of web.xml. That said, I would still prefer annotations over an additional configuration file :)

Upvotes: 3

Related Questions