我真的需要web.xml用于基于Servlet的Java Web应用程序吗? [英] Do I really need web.xml for a Servlet based Java web application?

查看:125
本文介绍了我真的需要web.xml用于基于Servlet的Java Web应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

提前谢谢!

推荐答案

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

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

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/*");
    }

}

这里是另一个显示如何使用其他可用注释的链接(@ ServletFilter,@ WebServletContextListener);您可以从这里下载规格表格。为了更详细地了解j2ee提供的注释。

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

这篇关于我真的需要web.xml用于基于Servlet的Java Web应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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