J2EE Maven Eclipse Hello World Tutorial Part Two

Hello World Beginner Tutorial using J2EE and Maven

This is part two of J2EE application with maven continued from Part One.

If you haven’t accomplished J2EE with Maven tutorial, please first complete it by going here

Maven relies highly on the pom.xml file that we would put on the project root folder. Lets do that.

In this part of J2EE tutorial and Maven tutorial, we will proceed from creating a pom.xml file which is the heart of maven.

3 Create the pom and compile the project using maven.

3.a put the following pom file in the MavenEclipseJ2EE directory.

<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/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.gullele</groupId>
<artifactId>MavenEclipseJ2EE</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Maven Eclipse J2EE tutorial</name>

<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.gullele.tutorial</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<url>http://localhost:8080/manager</url>
<username></username>
<password></password>
<path>/MavenEclipseJ2EE</path>
<warFile>target/MavenEclipseJ2EE.war</warFile>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- Needed for servlet part -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

<!-- Let's assume we need some logging and we chose this one -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version>
</dependency>

</dependencies>
</project>

3.b for the scope part, if the jar is included in the container, you can say provided and it will pick up. But, if you want to add the jar manually to maven then use:

First download servlet.jar – just google and you will get jar file and put it on your computer, then on your terminal

mvn install:install-file -Dfile=/path/to/servlet.jar -DgroupId=javax.servlet
-DartifactId=servlet-api -Dversion=2.5 -Dpackaging=jar

This command will add the jar to repository.

3.c Go to your terminal and being on the project’s directory, run this command
mvn compile

To see everything is going well.

4. Lets create a simple servlet

4.a Create a package com.gullele under src/main/java

4.b name the following file as MavenEclipseEEServlet.java and put in the created package.

package com.gullele.web;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * 
 * @author http://gullele.com
 *
 */
public class MavenEclipseEEServlet extends HttpServlet{
	protected void doGet(
            HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        try {

            PrintWriter out = response.getWriter();
            out.println("Maven J2ee and Eclipse tutorial");
            out.println("");
            out.flush();
            out.close();
        }
        catch (Exception ex) {
                throw new ServletException( ex );
        }
    }
}

4.c move the META-INF and WEB-INF folders along with their files to src/main/webapp

4.d update the web.xml in WEB-INF file as:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>MavenEclipseJ2EE</display-name>
<servlet>
<description>
</description>
<display-name>MavenEclipseEEServlet</display-name>
<servlet-name>MavenEclipseEEServlet</servlet-name>
<servlet-class>
com.gullele.web.MavenEclipseEEServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MavenEclipseEEServlet</servlet-name>
<url-pattern>/MavenEclipseEEServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

4.e create index.htm in the src/main/webapp/WEB-INF and put Wel come in it. This would show up as an index page.

Then that is pretty much it, we are done now.

The coolest command you would use to create the war file so as to deploy it would be
mvn war:war

This would create the war file in the target directory of the main project – you would put that in the tomcat and thats it.

When you go to your browser and type http://localhost:8080/MavenEclipseJ2EE/ you would get whatever you put on the index file and

when you do http://localhost:8080/MavenEclipseJ2EE/MavenEclipseEEServlet, you would get Maven J2ee and Eclipse tutorial

**Note, if you are trying this directly by running the server from the Eclipse, then make sure the web.xml file in MavenEclipseJ2EE/WebContent/WEB-INF/web.xml is the same with that of the one in src/main/webapp/WEB-INF/web.xml

Also deploying can be performed using tomcat’s manager. I will try to say some on my next blog.

The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path

AbstractSureFireMojo Class Missing Maven Surefire

Run Single test using Maven Java

String Ordered Permutation Algorithm Problem

setting JAVA_HOME on mac osx

hello world weblogic – hello world tutorial on weblogic

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*