Getting Started with Maven | Web Application Development | Creating a Web Application

Unlike ordinary Java applications, web applications require several additional considerations. The first is the program structure. In typical web application development, you prepare a directory to publish on the web and place JSP files and similar resources inside it. You also prepare a “WEB-INF” folder and place web.xml there. You need to create the project according to this directory structure.

Also, a web application cannot be run as-is. It requires a servlet container, commonly called a Java server. The server must be started, and the web application must be deployed there before it can be used.

For this reason, web application development requires a different creation method from ordinary Java application development.

Using maven-archetype-webapp

To create a web application project, use “maven-archetype-webapp.” Run the following command from a command prompt or terminal.

$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp

This uses the template for creating a web application project. For example, enter the following values.

Define value for property 'groupId': : com.devkuma
Define value for property 'artifactId': : SampleWebApp
Define value for property 'version':  1.0-SNAPSHOT: : 
Define value for property 'package':  com.devkuma: : 
  • Group ID: com.devkuma
  • Artifact ID: SampleWebApp
  • Version: 1.0-SNAPSHOT (default)
  • Package: com.tuyano.libro (default)

When input is complete, a project named SampleWebApp is created. You can use it as the basis for web application development.

Structure of the Generated Project

Now check how the generated project is structured. If you open the project folder, it has the following structure.

SampleMavenApp Folder

.
├── pom.xml
└── src
    └── main
        ├── resources
        └── webapp
            ├── WEB-INF
            │   └── web.xml
            └── index.jsp

Contents of the main Folder

Inside the src folder is the main folder. Instead of a java folder, it contains resources and webapp. It also does not include a test folder.

resources Folder

As the name suggests, the resources folder is where you place resource files needed by the web application project.

webapp Folder

This folder is the part published as the web application. JSP files and the WEB-INF folder are prepared inside it. HTML, CSS, JavaScript, and similar files can be placed there.

What About Servlets?

Although this is a Java project, there is no java folder. Some may wonder what to do when creating servlets and similar classes.

There is no java folder, but the basic approach to writing programs is the same. Prepare a java folder under the main folder, then place package folders and source code files inside it.

Generated pom.xml

Now check the pom.xml generated for the project. The default pom.xml is shown below.

<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>com.devkuma</groupId>
  <artifactId>SampleWebApp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SampleWebApp 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>
  </dependencies>
  <build>
    <finalName>SampleWebApp</finalName>
  </build>
</project>

As you can see, pom.xml itself is almost the same as the pom.xml for an ordinary Java application.

First, check whether development can proceed correctly. Run the following command.

$ mvn package

When it runs, a target folder is created, and a SampleWebApp.war file is generated inside it. If you extract this file, you can see that it contains the contents of the webapp folder. Also, if you created a java folder under main and prepared source code, a classes folder is placed under WEB-INF, and the class files are included there. At this point, you can see that the basic parts of the web application have been created correctly.

With this, if you create a war file with Maven commands and deploy it to a Java server, you can develop the application sufficiently.