Working with Tomcat7 Maven Plugin

In this article we are going to create a simple web application with the help of maven-archetype-webapp and will learn how to “Working with Tomcat7 Maven Plugin”. I hope you have read my previous article on how to integrate maven plugin with eclipse IDE and also how to create a standalone project with maven. The motto behind this article is just to demonstrate a maven based web-application. After creating the web application, you can deploy the same on any web server that you might have configured in eclipse or outside eclipse.

While developing web applications, we frequently need to test it to see if everything is working fine. Deploying the web application by creating the war file may be boring and also time consuming. Don’t worry, we have one awesome plugin called tomcat-maven-plugin. This plugin will start one instance of embedded tomcat server and web-application will be deployed automatically. Isn’t it interesting? Let us start by creating a simple web application.

Open File menu -> New -> Maven Project -> Click on Next

You will be asked to select an archetype, select maven-archetype-webapp.

Specify archetype parameters as following.

code-adda Working with Tomcat7 Maven Plugin

Hit Finish button. Your maven project will be created. You will notice that a small red cross box appears in the projects name as

While creating the web application, a default index.jsp page is also created. This is because project’s pom.xml only contains Junit dependency but not servlet dependency, so Red Cross Box appears. So we have to add Java Servlet API related dependency. Now let us start to develop the application. Follow the below steps. Projects directory structure should look like the following.

Step 1: Create a servlet with name GreetServlet.java under com.codeAdda.servlet package. Copy the following code in GreetServlet.java

In GreetServlet.java

[code lang=”java”] package com.codeAdda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GreetServlet extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {

// general setting
res.setContentType("text/html");
PrintWriter pw = res.getWriter();

Calendar calendar = Calendar.getInstance();
int hr = calendar.get(Calendar.HOUR_OF_DAY);

Date d = new Date();
pw.println("<b><i><center> Date and Time is " + d + " </b></i></center>");

if (hr <= 12)
pw.println("
<h2>Good Morning! Have A Nice Day</h2>

");

else if (hr <= 16)
pw.println("
<h2>Good Afternoon! How Are You Doing?</h2>

");

else if (hr <= 20)
pw.println("
<h2>Good Evening! How Was Your Day?</h2>

");

else {
pw.println("
<h2>Good night, Sleep Tight</h2>

");
}

pw.close();
}// service(-,-)
}// class
[/code]

Step 2: Every servlet must be configured in the web.xml. So, configure the servlet in web.xml like the following.

[code lang=”xml”] <!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>
<servlet>
<servlet-name>greetMe</servlet-name>
<servlet-class>com.codeAdda.servlet.GreetServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>greetMe</servlet-name>
<url-pattern>/greet</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
[/code]

As you can see, in the first <servlet> tag, we have supplied fully qualified class name of the servlet “GreetServlet” along with servlet name and in second <servlet> tag, we have provided the URL pattern to access the servlet.

Step 3: Now we have to supply all the required dependencies in the GreetingsWebApp/pom.xml as the following.

[code lang=”xml”] <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.codeAdda.servlet</groupId>
<artifactId>GreetingsWebApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>GreetingsWebApp 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.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>GreetingsWebApp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
[/code]

Execution:

We will now execute the above web application in different ways.

  1. Running the application as normal Run on Server:

I assume you have already configured any web server with your eclipse IDE. Right click on the GreetingsWebApp application -> Run As -> Run on Server -> Select the server you have configured and click on Next button -> Add GreetingsWebApp to configured section and click on Finish. Now the deployment process will start and you will be able to access the web application through any browser of your choice. Open any browser and hit with the following URL.

http://localhost:4040/GreetingsWebApp/

Here 4040 is the port I have configured. In your case it may vary. Now you will be able to access the web page. Navigate on the given Url and you will receive the greeting message.

2. Configure maven tomcat plugin in xml and use Run As -> Maven build.. goals=tomcat7:run

We can even work with Tomcat7 Maven Plugin which starts the in memory tomcat server and deploy the generated war file automatically. For this we have to use Tomcat7 Maven Plugin and provide its configuration in pom.xml.

Place the following piece of code in Greetings/pom.xml after the <plugin> tag

In GreetingsWebApp/pom.xml

[code lang=”xml”] <!– Configure Maven Tomcat Plugin –>
<!– http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin –>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<!– Configure contextPath and Port (Default – /GreetingsWebApp : 8080) –>
<configuration>
<server>tomcat</server>
<url>http://localhost:2020/manager</url>
<path>/GreetingsWebApp</path>
</configuration>
</plugin>
[/code]

During deployment, this configuration tells Maven to deploy the WAR file to embedded tomcat server via “http://localhost:2020/manager/”, on path “/GreetingsWebApp“.

How to execute?

Right Click on GreetingsWebApp -> Run As -> Maven Build.. ->

Specify goals= tomcat7:run -Dmaven.tomcat.port=9080   

Note: While executing, we have to mention the port number on which it will run.

After executing this command, if we get the following message in the command prompt

INFO: Starting ProtocolHandler [“http-bio-9080”]

then open any browser of your choice and run the web-application (http://localhost:9080/GreetingsWebApp)

Now let me show you other way to run the same application by specifying the port in configuration itself. Rather than specifying the port number in command prompt, we can do it through pom.xml also with the following configurations.

Following is the full configuration that should be in GreetingsWebApp/pom.xml

[code lang=”xml”] <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.codeAdda.servlet</groupId>
<artifactId>GreetingsWebApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>GreetingsWebApp 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.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>GreetingsWebApp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<!– Configure Maven Tomcat Plugin –>
<!– http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin –>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<!– Configure contextPath and Port (Default – /GreetingsWebApp : 8080) –>
<configuration>
<server>tomcat</server>
<url>http://localhost:2020/manager</url>
<port>9080</port>
<path>/GreetingsWebApp</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
[/code]

How to execute?

Follow the same process to run the web application as maven build. This time just specify the goals = tomcat7:run

I hope you have enjoyed this whole process of project development and deployment. Kindly subscribe to our blog and as always in case of any problem ping me in the comment section.

You can also read our another article based on Maven :

How to install maven in eclipse
Configuring Proxy In Maven (Part-4)
Repository Utilization In Maven (Part-3)

 

Leave a Reply