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"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


<name>Maven Eclipse J2EE tutorial</name>


<!-- Needed for servlet part -->

<!-- Let's assume we need some logging and we chose this one -->


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");
        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">

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 *