giliev
giliev

Reputation: 3058

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

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.

Thank you in advance!

Upvotes: 39

Views: 39940

Answers (7)

Vimal Panchal
Vimal Panchal

Reputation: 301

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

Upvotes: 1

Aniket Thakur
Aniket Thakur

Reputation: 68915

Another way (Spring 3.1+) -

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

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[] { "/" };
    }

}

Upvotes: 3

Giovanni
Giovanni

Reputation: 4015

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.

Upvotes: 15

Naman
Naman

Reputation: 2203

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.

Upvotes: 0

Chris
Chris

Reputation: 68

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.

Upvotes: 2

Mike Thomsen
Mike Thomsen

Reputation: 37506

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.

Upvotes: 12

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.

Upvotes: 0

Related Questions