Maven webapp

Maven webapplications are usually not executed by starting the JVM directly. Instead, an application server like Apache Tomcat is started in which webapplications are deployed. An application server provides services to operators who want to configure webapplications without rebuilding them.

Webapplications do network communication using the Hypertext Transfer Protocol (HTTP). First, update the Main class of subsection Maven basics to talk HTTP:

  1. Undo the changes of subsection Execute program by restoring the copy you made at the beginning of that subsection.

  2. Change file work/src/main/java/org/frankframework/maven/webapp/example/Main.java to become as follows:

    package org.wearefrank.maven.webapp.example;
    
    import org.apache.commons.lang3.StringUtils;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class Main extends HttpServlet {
        public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {
            PrintWriter pw = res.getWriter();
            pw.println(StringUtils.upperCase("Hello World!"));
            pw.close();
        }
    }
    
  3. Link this servlet to the URL that should trigger it. Do so by creating file work/src/main/webapp/WEB-INF/web.xml and give it the following contents:

    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
        <servlet>
            <servlet-name>hello</servlet-name>
            <servlet-class>org.wearefrank.maven.webapp.example.Main</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/api/hello</url-pattern>
        </servlet-mapping>
    </web-app>
    

Next, the pom.xml is updated:

  1. Update work/pom.xml to tell Maven that a .war file should be produced. Unlike the .jar produced in the previous subsection, the .war file includes all dependencies. Update the file as shown:

    ...
    <groupId>org.wearefrank</groupId>
    <artifactId>mavenWebappExample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    
    <dependencies>
        <dependency>
    ...
    
  2. Add an additional dependency:

    ...
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
    ...
    

The Java compiler has to resolve the import of class javax.servlet.http.HttpServlet and some other classes. These are found in artifact javax.servlet. Therefore, this artifact is added as a dependency. The artifat does not have to be packaged in the .war however, because every application server should provide the classes of this artifact. This common feature of application servers is required by the Jakarta EE Servlet specification. It explains why there is an additional line <scope>provided</scope> in the pom.xml.

The webapplication is done. It can be executed as follows:

  1. On a command prompt in directory work, run mvn clean install. A file target/mavenWebappExample-1.0-SNAPSHOT.war should be generated.

  2. Download and unzip Apache Tomcat from https://tomcat.apache.org/download-90.cgi. These instructions were tested on January 30 2024 for Tomcat version 9.0.85. In that test, the source code was built with Java 11.

  3. Go to the root directory of the unzipped Tomcat download, for example C:\Users\martijn\temp\apache-tomcat-9.0.85.

  4. That directory has subdirectory webapps. Put your .war file inside subdirectory webapps and name it mavenWebappExample.war.

  5. The Tomcat installation directory also has a subdirectory bin. Enter that directory.

  6. Start Tomcat by running startup.bat (under Windows).

  7. Open a webbrowser with URL http://localhost:8080/mavenWebappExample/api/hello. This should produce a webpage with the text HELLO WORLD!, the output from the servlet in class Main.java. The number 8080 is the port number. The URL /api/hello has been configured in web.xml by tag <url-pattern>.

The Frank!Framework is a Maven webapplication. You have a basic understanding now of what that means. Please study Frank2Example4 at https://github.com/wearefrank/frank-runner now to see how to package Frank configurations inside .war files for deployment. That code can be executed using the Frank!Runner, but it also shows how to organize your files for execution in production without the Frank!Runner.