1

I am new to Servlets. I am going through the basics and i discovered that even i put the name of different servlest in these tags

 <servlet-name>  </servlet-name>
    <servlet-class>  </servlet-class>

then also it works fine.

I want to know that what's the importance of presence of web.xml in a servlet project and how does it controls the execution of servlets ?

asad_hussain
  • 1,959
  • 1
  • 17
  • 27

2 Answers2

2

For a Java servlet to be accessible from a browser, you must tell the servlet container what servlets to deploy, and what URL's to map the servlets to. This is done in the web.xml file of your Java web application.

Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75
  • but i want to know that even i dont mention the name of a servlet in web.xml ,then also that servlet is deployed by the container when i run that servlet. How ?? – asad_hussain Mar 07 '16 at 17:12
2

If you are using annotations @WebServlet then you don't need to map your Servlet class in web.xml and probably that is why your app is working even if you put different name/servlet class in between <servlet-name/> <servlet-class/> tags.

You can either choose to map your all of your servlets in web.xml and don't use @WebServlet annotations at all. This will help to have a look at all of your servlet mappings in one file.

Or you can use @WebServlet annotation to map your Servlet classes. This is available since Servlet 3.0 spec.

For more information on web.xml see this answer.

Community
  • 1
  • 1
Sanjay Rawat
  • 2,304
  • 15
  • 30