在eclipse juno 4.2中创建Servlet时,不会自动获取web.xml文件 [英] not getting automatically web.xml file while creating servlet in eclipse juno 4.2

查看:202
本文介绍了在eclipse juno 4.2中创建Servlet时,不会自动获取web.xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 eclipse juno 4.2 java 1.7 tomcat 7 。但是在我的系统中,当我创建servlet时, web.xml 文件不会自动创建。但是,anothor系统自动创建 web .xml file.I我完全困惑,有什么要配置的。请帮帮我吗?

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?

我还添加了 web.xml 文件,当我要创建一个动态项目。

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

推荐答案

Tomcat 7是一个Servlet 3.0兼容容器。由于Servlet 3.0,servlet可以通过 @WebServlet 注释在类上进行配置,而不需要 web.xml 配置条目。仔细观察刚刚创建的servlet类,它上面有一个 @WebServlet 注释,其中包含您在新建Servlet 向导中指定的所有信息。

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.

有效地,这种配置servlet的新方式

Effectively, this new way of configuring servlets

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

与这种遗留方式完全相同配置servlet

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>

如果您仍然希望Eclipse创建一个 web.xml entry由于某些原因,那么您应该将动态Web模块版本从项目属性的项目方面部分的3.0更改为2.5。

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.

这篇关于在eclipse juno 4.2中创建Servlet时,不会自动获取web.xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆