Creating a simple Java Web project with Maven – part 1

Java beginners to create a simple web application (i.e. a Web ARchive filei.e. a war file). Prerequisite is that you have set up Java, Maven, and Eclipse.

 Step 1: Execute the Maven maven-archetype-webapp command to create the Maven web structure with the following command on a DOS command prompt. This assumes that you have set up Java and Maven.

Enter “0.1-SNAPSHOT” when prompted for the version and “Y” to package: com.mytutorial.

Step 2: You can verify the structure created in the file system as shown below. pom.xml is the maven file to define build details and web.xml is web deployment descriptor file.

Screen shot 2014-10-19 at 5.23.41 PM

Step 3: open the pom.xml file in a text editor like Notepad++. If you don’t already have it, download it. Add servlet-api as a dependency.

Step 4: Define a simple servlet class in a package com.mytutorial. You need to first create a folder named “java” under src/main. Then create folders com and com/mytutorial under the folder java. You can use the notepad++

Create a Java class named

Step 5: Go to the DOS command prompt and change directory to simpleWeb where the pom.xml file is. type the command mvn compile. This should compile your

Note that in the file system SimpleServlet.class file gets created under target/classes/com/mytutorial.

Step 6: You need to define the web description in the web.xml descriptor file.  You need to configure your Servlet details here.

Step 7: Java web applications are packaged as a war file. In order to package it, you need to run the Maven command mvn package.

Inspect your file system for the war file simpleWeb.war under target. If you are curious to see what files are inside, you can unzip it with a WinZip utility.

Step: This war file can be deployed to any web container or application server. For example, Tomcat, JBoss, Weblogic, etc. I deployed this to my local Weblogic server running on port 7002.

Where “simpleWeb” is the name of the war file you had created, and “simple” is the “url-pattern” you defined in your web.xml file.

Screen shot 2014-10-19 at 5.24.34 PM

A simple Java web tutorial without any IDE like eclipse. This also gives you an idea as to how Maven works.


500+ Enterprise & Core Java programmer & architect Q&As

Java & Big Data Tutorials