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

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

问题描述

我没有从事过真实世界的网络项目.在大学里,我们同时使用 Servlets 和 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.

先谢谢你!

推荐答案

如果您的容器支持最新的 j2ee 规范,则不需要 web.xml 文件.这里 是一个简单的 servlet 示例的链接,该示例使用注释和 here 你可以找到同样的 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.

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

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