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

7

39

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!

Revenge answered 15/5, 2015 at 12:7 Comment(10)
If you use Spring, you're unlikely to write any Servlets at all.Halt
Since the servlet 3 specification (IIRC) a web.xml is not needed anymore.Cluff
still depending on which application server he's gonna end up using, and to which extent he wants to use spring OP will be doomed to write not just a web.xml, but also another gazillion xml files. web.xml is one of the simplest.Anglesite
@Anglesite You haven't used Spring since 2.5, have you?Peria
@chrylis yes, and I'm still kinda traumatized by the amount of bloathed useless, complex and inconclusive xml crap I had to use. If they've simplified it, is good.Anglesite
@Anglesite Then you were doing it impressively wrong. The only Spring XML I have is for Web Flow, defining state machines, and Integration plans, which are very clear in XML.Peria
@chrylis can't exclude that, never used it before that time and you know, things should've been ready quickly, didn't have the time to go deep. The impression remains, too much configurations coupled with unorganized documentation. Hopefully they've improved it, if I'll have the need to use it again I will dig a bit more deeply into it.Anglesite
setting up a web.xml (copy-pasting and setting up a servlet) is not what I would call a great deal of work. you just steal a basic setup and adapt it in 5 minutes tops.Shifra
@Anglesite - don't worry, everything will always get more complicated over time:) creating syntax sugar to cover up complexity is great, it's just that you have one more thing to learn.Gaston
@bayou.io I sincerely hope so, I've been looking for a "just MVC" framework, and so far, none has completely satisfied me, there are good ones, but often they're kinda too much for the work we have to do.At the same time, we have ASP.NET MVC that fits that role perfectly, wonder why java counterparts de facto are so over-complex.Anglesite
U
15

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.

Unbowed answered 15/5, 2015 at 12:17 Comment(1)
The "latest" spec is called "Java EE". "J2EE" has been dropped since 2006.Sousaphone
S
12

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.

Skylab answered 15/5, 2015 at 12:29 Comment(2)
Hi but if we do not use xml then there might be some internal implementation of how actions are binded to servlets.Where is that?Yoghurt
@VikramSaini, the binding of actions to servlets is now done in annotations, like here: docs.spring.io/spring-boot/docs/1.5.8.RELEASE/reference/…Basrelief
H
3

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

}
Handclap answered 26/1, 2016 at 6:26 Comment(0)
C
2

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.

Constringe answered 15/5, 2015 at 12:30 Comment(0)
B
1

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

Babur answered 24/3, 2019 at 2:9 Comment(0)
P
0

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.

Peria answered 15/5, 2015 at 12:13 Comment(0)
H
0

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.

Healion answered 15/5, 2015 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.