reset password

Maven Web Application

Create an Maven web application in Eclipse using the maven-archetype-webapp archetype and do the following:

1. Change web.xml to the following:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0" id="springmvc">


    <display-name>Spring MVC Example</display-name>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

This change basically says that our project requires an application server that supports the Servlet 3.0 Specification.

2. Add the following to pom.xml (right after <finalName>):

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>

This change says that the project requires JDK 1.7 (or above) to compile, and the compiled class files are compatible with JVM 1.7.

3. Remove the junit dependency (we'll use TestNG instead), the add the following dependencies to pom.xml:

  • javax.servlet:javax.servlet-api:3.0.1 (set the scope of the dependency to provided)
  • javax.servlet:jstl:1.2

This allows us to use classes like HttpServletRequest and HttpServletResponse in our Java code, and use JSTL in the JSPs.

4. Add a folder src/main/java. This is where the Java code would go.

5. Right click on the project and select Maven -> Update Project...

This page has been viewed 20220 times.