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

后端 未结 7 1089
无人共我
无人共我 2020-12-03 06:26

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 alr

7条回答
  •  感动是毒
    2020-12-03 07:24

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

    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.

提交回复
热议问题