What is a web.xml and why we need web.xml?

In java web application development servlet or servlet life cycle is the key. The web.xml is the configuration file for a servlet-based java web application. This web.xml file is also called the deployment descriptor for the servlet-based Java web application.

Web.xml advise the servlet container or the web container (tomcat, jetty etc) which classes to load, what parameters to set in the context, which url should load which servlet and how to intercept requests coming from browsers.

  • What URLs we want to map for which servlet
  • listeners – classes that are notified when some events happen (context starts, session created, etc)
  • configuration parameters (context-params)
  • error pages, welcome files
  • servlet security constraints

In servlet 3.0 many of the web.xml parts are optional. These configurations can be done via annotations (@WebServlet, @WebListener)