Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

java - 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 another system it's create automatically web.xml file. I am totally confused, is there anything to configure?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...