How can Maven’s archetype webapp work without web.xml?

Asked

Viewed 110 times

3

I tried creating an app Maven archetype webapp 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 the web.xml (contrary to what is in the title of the question she has a web.xml, only this one has nothing interesting inside - and the question is, how does it locate /index.jsp when starting the 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 also has nothing special (follows below, 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-dá, it works! It finds the page index.jsp (that only has the phrase "Hello World")!

Hello World

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>

So it works smoothly, follows also an article with more details.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.