Reading:
Build and Deploy maven project with embedded tomcat

Build and Deploy maven project with embedded tomcat

Metamug
Build and Deploy maven project with embedded tomcat

In this article I am going to show you how you can easily create a maven project with embedded tomcat.

Generate a basic Maven Webproject project

You can generate, build and run Java web apps without the need of IDE like Eclipse or NetBeans. You can do this simply from the command line with maven. You don't need tomcat installed for this. You just need Java and Maven installations.

Generate with maven-archetype-webapp

mvn archetype:generate -DgroupId=com.metamug -DartifactId=mywebapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

The basic maven web project and that will create a file structure like this.

Create basic maven project

Add dependency

In the pom.xml file add these dependencies, these will be required to run Tomcat in an embedded mode.

<dependencies>
  <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>${tomcat.version}</version>
  </dependency>

  <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <version>${tomcat.version}</version>
  </dependency>
</dependencies>

Create a Main class

Now create a Main.java file in here src/main/java/launch and put this code inside it. This file will be required to run the application

The generated web.xml uses servlet API 2.3 This web.xml file should be replaced

import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import java.io.File;

public class Main {

    public static void main(String[] args) throws Exception {

        String webappDirLocation = "src/main/webapp/";
        Tomcat tomcat = new Tomcat();
        String webPort = System.getenv("PORT");

        if(webPort == null || webPort.isEmpty()) {
          webPort = "8080";
        }

        tomcat.setPort(Integer.valueOf(webPort));
        StandardContext ctx = (StandardContext) tomcat.addWebapp("", new
        File(webappDirLocation).getAbsolutePath());

        System.out.println("configuring app with basedir: " + new File("./" +
        webappDirLocation).getAbsolutePath());

        File additionWebInfClasses = new File("target/classes");

        WebResourceRoot resources = new StandardRoot(ctx);

        resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes",

        additionWebInfClasses.getAbsolutePath(), "/"));

        ctx.setResources(resources);

        tomcat.enableNaming();

        tomcat.getConnector();

        try {
          tomcat.start();
        } catch (LifecycleException e) {
          e.printStackTrace();
        }
        tomcat.getServer().await();

    }

}


Add a JSP file

Create an index.jsp file in src/main/webapp directory with this code in it. This will be the only page for our application and we will access this by running the Main.java file.

<html>
  <body>
    <h1>Hello Java!</h1>
  </body>
</html>

Now we are finished with creating our application. Run the application by running the Main.java file. You can access it by visiting this url “http://localhost:8080/”.


Add the JSTL Dependency to pom.xml


<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

After adding the dependency rerun the Main.java file. You can edit the generated index.jsp and you will see the changes to the browser. The below JSP code uses both JSTL and Expression Language to confirm the configuration is done correctly.


<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

  <c:set var="x" value="5" />
  <h4> The value of x is: <c:out value="${x}" /> </h4>


Final result will look like this JSP code output

I've uploaded the source code on github



Icon For Arrow-up
Comments

Post a comment