Chintamani Chintamani - 6 months ago 18
Java Question

not getting automatically web.xml file while creating servlet in eclipse juno 4.2

I am using eclipse juno 4.2,java 1.7 and tomcat 7.But in my system when i create servlet the web.xml file doesn't create automatically.but anothor system it's create automatically web.xml file.I am totally confused.Is there anything to configure.Please help me out??

I also add web.xml file when i am going to create a dynamic project...

Answer

Tomcat 7 is a Servlet 3.0 compatible container. Since Servlet 3.0, the servlets can be configured by @WebServlet annotation on the class without the need for a web.xml configuration entry. Look closer at the servlet class you just created, there's a @WebServlet annotation on it containing all information you specified in New Servlet wizard.

Effectively, this new way of configuring servlets

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {}

does exactly the same as this legacy way of configuring servlets

<servlet>
    <servlet-name>helloServlet</servlet-name>
    <servlet-class>com.example.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
</servlet-mapping>

If you still want Eclipse to create a web.xml entry for some reason, then you should change the Dynamic Web Module version back from 3.0 to 2.5 in Project Facets section of project's properties.