对于基于 Servlet 的 Java Web 应用程序,我真的需要 web.xml 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30259153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:23:32  来源:igfitidea点击:

Do I really need web.xml for a Servlet based Java web application?

javaservletsweb.xml

提问by giliev

I haven't been working on real world web projects. At university we used both Servlets and Spring for Java web development. In both projects we were given web.xml files already configured and we were doing only minor changes in them. Now I need to build a web app from a scratch. I created new Servlet class in Eclipse and it didn't automatically create any web.xml. Then I googled, and I read from several resources that web.xml is not really needed, but that reasoning was put in couple of sentences, so I am not sure if using annotations instead of web.xml will be no problem. I will be really glad if there is no need to configure web.xml, because I haven't configured one by myself and I want to focus more on the business logic.

我没有从事过真实世界的网络项目。在大学里,我们同时使用 Servlets 和 Spring 进行 Java Web 开发。在这两个项目中,我们都获得了已经配置好的 web.xml 文件,我们只对它们进行了微小的更改。现在我需要从头开始构建一个 Web 应用程序。我在 Eclipse 中创建了新的 Servlet 类,但它没有自动创建任何 web.xml。然后我用谷歌搜索,我从几个资源中读到 web.xml 并不是真正需要的,但是这个推理被放在几句话中,所以我不确定使用注释而不是 web.xml 是否没有问题。如果不需要配置web.xml,我会很高兴,因为我没有自己配置一个,我想更专注于业务逻辑。

Thank you in advance!

先感谢您!

回答by chrylis -cautiouslyoptimistic-

Use Spring Boot, which will manage the container and all the boilerplate configuration for you. You can generate a ready-to-launch skeleton with Initializr.

使用 Spring Boot,它将为您管理容器和所有样板配置。您可以使用Initializr生成一个随时可用的骨架。

回答by Giovanni

You don't need a web.xmlfile if you have a container that supports the latest j2ee specs. Hereis a link to an simple servlet example that use an annotation and hereyou can find the same for Spring MVC; I post the example here for you convenience

web.xml如果您有一个支持最新 j2ee 规范的容器,则不需要文件。 是一个使用注释的简单 servlet 示例的链接,您可以在此处找到 Spring MVC 的相同内容;为方便起见,我在此处发布示例

public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) {
        ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet());
        registration.setLoadOnStartup(1);
        registration.addMapping("/example/*");
    }

}

Hereis another link that show how to use the other annotations available(@ServletFilter, @WebServletContextListener); you can download the specs form herein order to get a more detailed view of the annotations available via j2ee.

是另一个链接,显示如何使用其他可用的注释(@ServletFilter, @WebServletContextListener); 您可以在此处下载规范表单,以便更详细地了解可通过 j2ee 获得的注释。

回答by Mike Thomsen

Starting in Servlet 3, no web.xml is required. You're going to want to use something like Tomcat 7 or 8 (better choice). For raw servlets this is a good starting point.

从 Servlet 3 开始,不需要 web.xml。您将想要使用 Tomcat 7 或 8(更好的选择)之类的东西。对于原始 servlet,这是一个很好的起点

If you want to use modern Spring, Grails 3 is a great way to go. It side steps all of these issues and Grails is a very productive framework for web development. You can think of it as Ruby on Rails built on top of Spring and Hibernate.

如果您想使用现代 Spring,Grails 3 是一个不错的选择。它回避了所有这些问题,Grails 是一个非常高效的 Web 开发框架。您可以将其视为构建在 Spring 和 Hibernate 之上的 Ruby on Rails。

At this point, you shouldn't have to write any web.xml to get set up unless you use a framework that needs it. I don't know about spring mvc, but Grails doesn't require you to do that and it uses most of what you're already used to using.

此时,除非您使用需要它的框架,否则您不必编写任何 web.xml 来进行设置。我不知道 spring mvc,但 Grails 不需要你这样做,它使用了你已经习惯使用的大部分内容。

回答by Naman P

No, there will be no need of web.xmlfor servlet based applicationif you are using servlet version >3.0and tomcat 7as it will not run in the previous versions of tomcat.

不,不会有需要web.xmlservlet based application,如果你使用的是Servlet版本>3.0,并tomcat 7因为它不会在Tomcat的以前的版本上运行。

Annotation represents the metadata. If you use annotation, deployment descriptor (web.xml file) is not required. Have a look Herefor all available annotation.

注释表示元数据。如果使用注解,则不需要部署描述符(web.xml 文件)。看看这里所有可用的注释。

回答by Chris

Whether or not you need web.xml is dependent on which servlet specification you claim in your application. If you will be building an app using spec 3.0, you can use annotations to declare your servlets, and deploy it to a container without needing a web.xml file. This was done as part of JSR-315.

您是否需要 web.xml 取决于您在应用程序中声明的 servlet 规范。如果您将使用规范 3.0 构建应用程序,则可以使用注释来声明您的 servlet,并将其部署到容器中,而无需 web.xml 文件。这是作为JSR-315 的一部分完成的。

回答by Aniket Thakur

Another way (Spring 3.1+) -

另一种方式(Spring 3.1+) -

An abstract base class implementation of WebApplicationInitializernamed AbstractDispatcherServletInitializermakes it even easier to register the DispatcherServlet by simply overriding methods to specify the servlet mapping and the location of the DispatcherServlet configuration -

WebApplicationInitializernamed的抽象基类实现AbstractDispatcherServletInitializer使注册 DispatcherServlet 变得更加容易,只需覆盖方法来指定 servlet 映射和 DispatcherServlet 配置的位置 -

public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {

    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }

    @Override
    protected WebApplicationContext createServletApplicationContext() {
        XmlWebApplicationContext cxt = new XmlWebApplicationContext();
        cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
        return cxt;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

回答by Vimal Panchal

Here I found an example of Web Application without using the deployment descriptor file(web.xml). The only point to consider here is this will work with the latest tomcat versions >=7.0

在这里,我找到了一个不使用部署描述符文件(web.xml)的 Web 应用程序示例。这里唯一需要考虑的是这将适用于最新的 tomcat 版本 >=7.0

Visit http://java-demos.blogspot.com/2014/01/servlet-web-application-without-webxml.html

访问http://java-demos.blogspot.com/2014/01/servlet-web-application-without-webxml.html

Visit https://www.baeldung.com/java-web-app-without-web-xml

访问https://www.baeldung.com/java-web-app-without-web-xml