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

后端 未结 7 991
无人共我
无人共我 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 06:59

    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.

    0 讨论(0)
  • 2020-12-03 07:08

    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.

    0 讨论(0)
  • 2020-12-03 07:14

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

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

    0 讨论(0)
  • 2020-12-03 07:14

    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

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

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

    0 讨论(0)
  • 2020-12-03 07:21

    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.

    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.

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题