How can Maven webapp archetype work without web.xml?

3

I tried to create a Maven archetype webapp application in Eclipse JEE Photon:

File > New > Maven Project > Next > maven-archetype-webapp 1.0

It generates the following file structure:

.
 |-- src
 |   '-- main
 |       '-- java
 |           |-- resources
 |           |-- webapp
 |           |   '-- WEB-INF
 |           |       '-- web.xml
 |           '-- index.jsp
  '-- pom.xml

This is web.xml (unlike what is in the title of the question it has a web.xml , except that it has nothing interesting inside it - and the question is, how does it find /index.jsp when starting application?):

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

The pom.xml is also nothing special (below, I just added the dependency Servlets 3.0.1):

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>piovezan</groupId>
    <artifactId>webportfolio2</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>webportfolio2 Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>webportfolio2</finalName>
    </build>
</project>

... and Ta-da, it works! It finds the page index.jsp (which only has the phrase "Hello World")!

    
asked by anonymous 02.08.2018 / 01:02

1 answer

1

To decrease the use of xml from Servlet 3.0 the use of web.xml is optional, as you can see in the archetype pom the dependency is above this:

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
</dependency>

For this reason, it works without problems, it also follows an article with more details .

    
13.11.2018 / 21:55