Building a web application with Maven

duration 15 minutes

Prerequisites:

Learn how to build and test a simple web application using Maven and Open Liberty.

What you’ll learn

You will learn how to configure a simple web servlet application using Maven and the Liberty Maven plugin. When you compile and build the application code, Maven downloads and installs Open Liberty. If you run the application, Maven creates an Open Liberty server and runs the application on it. The application displays a simple web page with a link that, when clicked, calls the servlet to return a simple response of Hello! How are you today?.

One benefit of using Maven is that, once you have defined the details of the project and any dependencies it has, Maven automatically handles downloading and installing any and all of the dependencies.

Another benefit of using Maven is that it can run automated tests on the application after building it. You could, of course, test your application manually by starting a server and pointing a web browser at the application URL. Automated tests are a much better approach though because Maven can easily re-run the same tests each time it builds the application. If the tests don’t pass after you’ve made a change to the application, the build fails so you know that you need to fix your code.

You will create a Maven build definition file (pom.xml) for the web application project and use it to build the web application. You will then create a simple, automated test and configure Maven to run it after building the application.

Installing Maven

If Maven isn’t already installed, download the binary zip or tar.gz file. Then, follow the installation instructions for your operating system to extract the .zip file and add the bin directory, which contains the mvn command to the PATH on your computer.

Run the following command to test that Maven is installed:

mvn -v

If Maven is installed properly, you see information about the Maven installation similar to the following example:

Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T20:39:06+01:00)
Maven home: /Applications/Maven/apache-maven-3.5.0
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
Default locale: en_GB, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.6", arch: "x86_64", family: "mac"

Getting started

The fastest way to work through this guide is to clone the Git repository and use the projects that are provided inside:

git clone https://github.com/openliberty/guide-maven-intro.git
cd guide-maven-intro

The start directory contains the starting project that you will build upon.

The finish directory contains the finished project that you will build.

Try what you’ll build

The finish directory in the root of this guide contains the finished application. Give it a try before you proceed.

To try out the application, first go to the finish directory and run the following Maven goal to build the application and deploy it to Open Liberty:

mvn liberty:run

After you see the following message, your application server is ready.

The defaultServer server is ready to run a smarter planet.

Navigate your browser to the http://localhost:9080/ServletSample/servlet URL to access the application. The servlet returns a simple response of Hello! How are you today?.

After you are finished checking out the application, stop the Open Liberty server by pressing CTRL+C in the shell session where you ran the server. Alternatively, you can run the liberty:stop goal from the finish directory in another shell session:

mvn liberty:stop

Creating a simple application

The simple web application that you will build using Maven and Open Liberty is provided for you in the start directory so that you can focus on learning about Maven. The application uses the standard Maven directory structure. Using the standard directory structure saves you from customizing the pom.xml file later.

All the application source code, including the Open Liberty server configuration (server.xml), is in the src/main/liberty/config directory:

    └── src
        └── main
           └── java
           └── resources
           └── webapp
           └── liberty
                  └── config

Creating the project POM file

Navigate to the start directory to begin.

Before you can build the project, you define the Maven Project Object Model (POM) file, the pom.xml.

Create the pom.xml file.
pom.xml

pom.xml

  1<?xml version='1.0' encoding='utf-8'?>
  2<!-- tag::project[] -->
  3<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">
  4<!-- end::project[] -->
  5    <!-- tag::modelVersion[] -->
  6    <modelVersion>4.0.0</modelVersion>
  7    <!-- end::modelVersion[] -->
  8
  9    <groupId>io.openliberty.guides</groupId>
 10    <!-- tag::artifactID[] -->
 11    <artifactId>ServletSample</artifactId>
 12    <!-- end::artifactID[] -->
 13    <!-- tag::packaging[] -->
 14    <packaging>war</packaging>
 15    <!-- end::packaging[] -->
 16    <version>1.0-SNAPSHOT</version>
 17
 18    <!-- tag::properties[] -->
 19    <properties>
 20        <!-- tag::encoding[] -->
 21        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 22        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 23        <!-- end::encoding[] -->
 24        <!-- tag::java-version[] -->
 25        <maven.compiler.source>1.8</maven.compiler.source>
 26        <maven.compiler.target>1.8</maven.compiler.target>
 27        <!-- end::java-version[] -->
 28        <!-- Liberty configuration -->
 29        <!-- tag::default-http-port[] -->
 30        <liberty.var.default.http.port>9080</liberty.var.default.http.port>
 31        <!-- end::default-http-port[] -->
 32        <liberty.var.default.https.port>9443</liberty.var.default.https.port>
 33        <liberty.var.app.context.root>${project.artifactId}</liberty.var.app.context.root>
 34    </properties>
 35    <!-- end::properties[] -->
 36
 37    <!-- tag::dependencies[] -->
 38    <dependencies>
 39        <!-- Provided dependencies -->
 40        <dependency>
 41            <groupId>jakarta.platform</groupId>
 42            <artifactId>jakarta.jakartaee-api</artifactId>
 43            <version>8.0.0</version>
 44            <scope>provided</scope>
 45        </dependency>
 46        <dependency>
 47            <groupId>org.eclipse.microprofile</groupId>
 48            <artifactId>microprofile</artifactId>
 49            <version>3.3</version>
 50            <type>pom</type>
 51            <scope>provided</scope>
 52        </dependency>
 53        <!-- For testing -->
 54        <!-- tag::commons-httpclient[] -->
 55        <dependency>
 56            <groupId>commons-httpclient</groupId>
 57            <artifactId>commons-httpclient</artifactId>
 58            <version>3.1</version>
 59            <!-- tag::test1[] -->
 60            <scope>test</scope>
 61            <!-- end::test1[] -->
 62        </dependency>
 63        <!-- end::commons-httpclient[] -->
 64        <!-- tag::junit[] -->
 65        <dependency>
 66            <groupId>org.junit.jupiter</groupId>
 67            <artifactId>junit-jupiter</artifactId>
 68            <version>5.6.2</version>
 69            <!-- tag::test2[] -->
 70            <scope>test</scope>
 71            <!-- end::test2[] -->
 72        </dependency>
 73        <!-- end::junit[] -->
 74        <!-- Support for JDK 9 and above -->
 75        <!-- tag::javax-servlet-api[] -->
 76        <dependency>
 77            <!-- tag::groupID-servlet-api[] -->
 78            <groupId>javax.servlet</groupId>
 79            <!-- end::groupID-servlet-api[] -->
 80            <!-- tag::artifactID-servlet-api[] -->
 81            <artifactId>javax.servlet-api</artifactId>
 82            <!-- end::artifactID-servlet-api[] -->
 83            <!-- tag::version-servlet-api[] -->
 84            <version>4.0.1</version>
 85            <!-- end::version-servlet-api[] -->
 86            <!-- tag::scope-servlet-api[] -->
 87            <scope>provided</scope>
 88            <!-- end::scope-servlet-api[] -->
 89        </dependency>
 90        <!-- end::javax-servlet-api[] -->
 91    </dependencies>
 92    <!-- end::dependencies[] -->
 93
 94    <!-- tag::build[] -->
 95    <build>
 96        <finalName>${project.artifactId}</finalName>
 97        <plugins>
 98            <plugin>
 99                <groupId>org.apache.maven.plugins</groupId>
100                <artifactId>maven-war-plugin</artifactId>
101                <version>3.2.3</version>
102            </plugin>
103            <!-- tag::liberty-maven-plugin[] -->
104            <plugin>
105                <groupId>io.openliberty.tools</groupId>
106                <artifactId>liberty-maven-plugin</artifactId>
107                <version>3.2</version>
108                <!-- tag::configuration[] -->
109                <configuration>
110                    <!-- tag::serverName[] -->
111                    <serverName>guideServer</serverName>
112                    <!-- end::serverName[] -->
113                </configuration>
114                <!-- end::configuration[] -->
115            </plugin>
116            <!-- end::liberty-maven-plugin[] -->
117            <!-- tag::maven-failsafe-plugin[] -->
118            <plugin>
119                <groupId>org.apache.maven.plugins</groupId>
120                <artifactId>maven-failsafe-plugin</artifactId>
121                <version>2.22.2</version>
122                <configuration>
123                    <!-- tag::system-property-variables[] -->
124                    <systemPropertyVariables>
125                        <!-- tag::http-port[] -->
126                        <http.port>${liberty.var.default.http.port}</http.port>
127                        <!-- end::http-port[] -->
128                        <!-- tag::war-name[] -->
129                        <war.name>${liberty.var.app.context.root}</war.name>
130                        <!-- end::war-name[] -->
131                    </systemPropertyVariables>
132                    <!-- end::system-property-variables[] -->
133                </configuration>
134            </plugin>
135            <!-- end::maven-failsafe-plugin[] -->
136        </plugins>
137    </build>
138    <!-- end::build[] -->
139</project>

The pom.xml file starts with a root <project/> element and a <modelversion/> element, which is always set to 4.0.0.

A typical POM for a Liberty application contains the following sections:

  • Project coordinates: The identifiers for this application.

  • Properties (<properties/>): Any properties for the project go here, including compilation details and any values that are referenced during compilation of the Java source code and generating the application.

  • Dependencies (<dependencies/>): Any Java dependencies that are required for compiling, testing, and running the application are listed here.

  • Build plugins (<build/>): Maven is modular and each of its capabilities is provided by a separate plugin. This is where you specify which Maven plugins should be used to build this project and any configuration information needed by those plugins.

The project coordinates describe the name and version of the application. The artifactId gives a name to the web application project, which is used to name the output files that are generated by the build (e.g. the WAR file) and the Open Liberty server that is created. You’ll notice that other fields in the pom.xml file use variables that are resolved by the artifactId field. This is so that you can update the name of the sample application, including files generated by Maven, in a single place in the pom.xml file. The value of the packaging field is war so that the project output artifact is a WAR file.

The first four properties in the properties section of the project, just define the encoding (UTF-8) and version of Java (Java 8) that Maven uses to compile the application source code.

Open Liberty configuration properties provide you with a single place to specify values that are used in multiple places throughout the application. For example, the default.http.port value is used in both the server configuration (server.xml) file and will be used in the test class that you will add (EndpointIT.java) to the application. Because the default.http.port value is specified in the pom.xml file, you can easily change the port number that the server runs on without updating the application code in multiple places.

HelloServlet.java

 1// tag::comment[]
 2/*******************************************************************************
 3 * Copyright (c) 2017, 2019 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License v1.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-v10.html
 8 *
 9 * Contributors:
10 *     IBM Corporation - initial API and implementation
11 *******************************************************************************/
12 // end::comment[]
13package io.openliberty.guides.hello;
14
15import java.io.IOException;
16
17import javax.servlet.ServletException;
18import javax.servlet.annotation.WebServlet;
19import javax.servlet.http.HttpServlet;
20import javax.servlet.http.HttpServletRequest;
21import javax.servlet.http.HttpServletResponse;
22
23@WebServlet(urlPatterns="/servlet")
24public class HelloServlet extends HttpServlet {
25    private static final long serialVersionUID = 1L;
26
27    // tag::javadoc1[]
28    /**
29     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
30     */
31    // end::javadoc1[]
32    protected void doGet(HttpServletRequest request, HttpServletResponse response)
33        throws ServletException, IOException {
34        // tag::responseString[]
35        response.getWriter().append("Hello! How are you today?\n");
36        // end::responseString[]
37    }
38
39    // tag::javadoc2[]
40    /**
41     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
42     */
43    // end::javadoc2[]
44    protected void doPost(HttpServletRequest request, HttpServletResponse response)
45        throws ServletException, IOException {
46        doGet(request, response);
47    }
48}

The HelloServlet.java class depends on javax.servlet-api to compile. Maven will download this dependency from the Maven Central repository using the groupId, artifactId, and version details that you provide here. The dependency is set to provided, which means that the API is in the server runtime and doesn’t need to be packaged by the application.

The <build/> section gives details of the two plugins that Maven uses to build this project.

  • The Maven plugin for generating a WAR file as one of the output files.

  • The Liberty Maven plug-in, which allows you to install applications into Open Liberty and manage the server instances.

In the liberty-maven-plugin plug-in section, you can add a <configuration/> element to specify Open Liberty configuration details. For example, the <serverName/> field defines the name of the Open Liberty server that Maven creates. You specified guideServer as the value for <serverName/>. If the <serverName/> field is not included, the default value is defaultServer.

server.xml

 1<server description="Sample Servlet server">
 2    <featureManager>
 3        <feature>servlet-4.0</feature>
 4    </featureManager>
 5
 6    <variable name="default.http.port" defaultValue="9080"/>
 7    <variable name="default.https.port" defaultValue="9443"/>
 8    <variable name="app.context.root" defaultValue="ServletSample"/>
 9
10    <!-- tag::httpEndpoint[] -->
11    <httpEndpoint httpPort="${default.http.port}"
12    httpsPort="${default.https.port}" id="defaultHttpEndpoint"  host="*" />
13    <!-- end::httpEndpoint[] -->
14    <webApplication id="ServletSample" location="ServletSample.war" contextRoot="${app.context.root}" />
15</server>

Running the application

When you run Open Liberty in dev mode, the server listens for file changes and automatically recompiles and deploys your updates whenever you save a new change. Run the following goal to start in dev mode:

mvn liberty:dev

After you see the following message, your application server in dev mode is ready:

Press the Enter key to run tests on demand.

Dev mode holds your command line to listen for file changes. Open another command line to continue, or open the project in your editor.

Navigate your browser to the http://localhost:9080/ServletSample/servlet URL to access the application. The servlet returns a simple response of Hello! How are you today?.

Testing the web application

One of the benefits of building an application with Maven is that Maven can be configured to run a set of tests. You can write tests for the individual units of code outside of a running application server (unit tests), or you can write them to call the application server directly (integration tests). In this example you will create a simple integration test that checks that the web page opens and that the correct response is returned when the link is clicked.

Create the EndpointIT class.
src/test/java/io/openliberty/guides/hello/it/EndpointIT.java

EndpointIT.java

 1// tag::copyright[]
 2/*******************************************************************************
 3 * Copyright (c) 2017, 2019 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License v1.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-v10.html
 8 *
 9 * Contributors:
10 *     IBM Corporation - initial API and implementation
11 *******************************************************************************/
12// end::copyright[]
13package io.openliberty.guides.hello.it;
14
15// tag::import[]
16import static org.junit.jupiter.api.Assertions.*;
17import org.junit.jupiter.api.BeforeAll;
18import org.junit.jupiter.api.Test;
19import org.apache.commons.httpclient.HttpClient;
20import org.apache.commons.httpclient.HttpStatus;
21import org.apache.commons.httpclient.methods.GetMethod;
22// end::import[]
23
24// tag::EndpointIT[]
25public class EndpointIT {
26    private static String URL;
27
28    @BeforeAll
29    public static void init() {
30        // tag::URL[]
31        String port = System.getProperty("http.port");
32        String war = System.getProperty("war.name");
33        URL = "http://localhost:" + port + "/" + war + "/" + "servlet";
34        // end::URL[]
35    }
36
37    // tag::Test[]
38    @Test
39    // end::Test[]
40    public void testServlet() throws Exception {
41        HttpClient client = new HttpClient();
42
43        GetMethod method = new GetMethod(URL);
44        // tag::link[]
45        try {
46            int statusCode = client.executeMethod(method);
47
48            assertEquals(HttpStatus.SC_OK, statusCode, "HTTP GET failed");
49
50            String response = method.getResponseBodyAsString(1000);
51
52            assertTrue(response.contains("Hello! How are you today?"),
53                "Unexpected response body");
54        } finally {
55            method.releaseConnection();
56        }
57        // end::link[]
58    }
59}
60// end::EndpointIT[]

The test class name ends in IT to indicate that it contains an integration test.

Maven is configured to run the integration test using the maven-failsafe-plugin. The <systemPropertyVariables/> section defines some variables that the test class uses. The test code needs to know where to find the application that it is testing. While the port number and context root information can be hardcoded in the test class, it is better to specify it in a single place like the Maven pom.xml file because this information is also used by other files in the project. The <systemPropertyVariables/> section passes these details to the Java test program as a series of system properties, resolving the http.port and war.name variables.

pom.xml

  1<?xml version='1.0' encoding='utf-8'?>
  2<!-- tag::project[] -->
  3<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">
  4<!-- end::project[] -->
  5    <!-- tag::modelVersion[] -->
  6    <modelVersion>4.0.0</modelVersion>
  7    <!-- end::modelVersion[] -->
  8
  9    <groupId>io.openliberty.guides</groupId>
 10    <!-- tag::artifactID[] -->
 11    <artifactId>ServletSample</artifactId>
 12    <!-- end::artifactID[] -->
 13    <!-- tag::packaging[] -->
 14    <packaging>war</packaging>
 15    <!-- end::packaging[] -->
 16    <version>1.0-SNAPSHOT</version>
 17
 18    <!-- tag::properties[] -->
 19    <properties>
 20        <!-- tag::encoding[] -->
 21        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 22        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 23        <!-- end::encoding[] -->
 24        <!-- tag::java-version[] -->
 25        <maven.compiler.source>1.8</maven.compiler.source>
 26        <maven.compiler.target>1.8</maven.compiler.target>
 27        <!-- end::java-version[] -->
 28        <!-- Liberty configuration -->
 29        <!-- tag::default-http-port[] -->
 30        <liberty.var.default.http.port>9080</liberty.var.default.http.port>
 31        <!-- end::default-http-port[] -->
 32        <liberty.var.default.https.port>9443</liberty.var.default.https.port>
 33        <liberty.var.app.context.root>${project.artifactId}</liberty.var.app.context.root>
 34    </properties>
 35    <!-- end::properties[] -->
 36
 37    <!-- tag::dependencies[] -->
 38    <dependencies>
 39        <!-- Provided dependencies -->
 40        <dependency>
 41            <groupId>jakarta.platform</groupId>
 42            <artifactId>jakarta.jakartaee-api</artifactId>
 43            <version>8.0.0</version>
 44            <scope>provided</scope>
 45        </dependency>
 46        <dependency>
 47            <groupId>org.eclipse.microprofile</groupId>
 48            <artifactId>microprofile</artifactId>
 49            <version>3.3</version>
 50            <type>pom</type>
 51            <scope>provided</scope>
 52        </dependency>
 53        <!-- For testing -->
 54        <!-- tag::commons-httpclient[] -->
 55        <dependency>
 56            <groupId>commons-httpclient</groupId>
 57            <artifactId>commons-httpclient</artifactId>
 58            <version>3.1</version>
 59            <!-- tag::test1[] -->
 60            <scope>test</scope>
 61            <!-- end::test1[] -->
 62        </dependency>
 63        <!-- end::commons-httpclient[] -->
 64        <!-- tag::junit[] -->
 65        <dependency>
 66            <groupId>org.junit.jupiter</groupId>
 67            <artifactId>junit-jupiter</artifactId>
 68            <version>5.6.2</version>
 69            <!-- tag::test2[] -->
 70            <scope>test</scope>
 71            <!-- end::test2[] -->
 72        </dependency>
 73        <!-- end::junit[] -->
 74        <!-- Support for JDK 9 and above -->
 75        <!-- tag::javax-servlet-api[] -->
 76        <dependency>
 77            <!-- tag::groupID-servlet-api[] -->
 78            <groupId>javax.servlet</groupId>
 79            <!-- end::groupID-servlet-api[] -->
 80            <!-- tag::artifactID-servlet-api[] -->
 81            <artifactId>javax.servlet-api</artifactId>
 82            <!-- end::artifactID-servlet-api[] -->
 83            <!-- tag::version-servlet-api[] -->
 84            <version>4.0.1</version>
 85            <!-- end::version-servlet-api[] -->
 86            <!-- tag::scope-servlet-api[] -->
 87            <scope>provided</scope>
 88            <!-- end::scope-servlet-api[] -->
 89        </dependency>
 90        <!-- end::javax-servlet-api[] -->
 91    </dependencies>
 92    <!-- end::dependencies[] -->
 93
 94    <!-- tag::build[] -->
 95    <build>
 96        <finalName>${project.artifactId}</finalName>
 97        <plugins>
 98            <plugin>
 99                <groupId>org.apache.maven.plugins</groupId>
100                <artifactId>maven-war-plugin</artifactId>
101                <version>3.2.3</version>
102            </plugin>
103            <!-- tag::liberty-maven-plugin[] -->
104            <plugin>
105                <groupId>io.openliberty.tools</groupId>
106                <artifactId>liberty-maven-plugin</artifactId>
107                <version>3.2</version>
108                <!-- tag::configuration[] -->
109                <configuration>
110                    <!-- tag::serverName[] -->
111                    <serverName>guideServer</serverName>
112                    <!-- end::serverName[] -->
113                </configuration>
114                <!-- end::configuration[] -->
115            </plugin>
116            <!-- end::liberty-maven-plugin[] -->
117            <!-- tag::maven-failsafe-plugin[] -->
118            <plugin>
119                <groupId>org.apache.maven.plugins</groupId>
120                <artifactId>maven-failsafe-plugin</artifactId>
121                <version>2.22.2</version>
122                <configuration>
123                    <!-- tag::system-property-variables[] -->
124                    <systemPropertyVariables>
125                        <!-- tag::http-port[] -->
126                        <http.port>${liberty.var.default.http.port}</http.port>
127                        <!-- end::http-port[] -->
128                        <!-- tag::war-name[] -->
129                        <war.name>${liberty.var.app.context.root}</war.name>
130                        <!-- end::war-name[] -->
131                    </systemPropertyVariables>
132                    <!-- end::system-property-variables[] -->
133                </configuration>
134            </plugin>
135            <!-- end::maven-failsafe-plugin[] -->
136        </plugins>
137    </build>
138    <!-- end::build[] -->
139</project>

The following lines in the EndpointIT test class uses these system variables to build up the URL of the application.

In the test class, after defining how to build the application URL, the @Test annotation indicates the start of the test method.

In the try block of the test method, an HTTP GET request to the URL of the application returns a status code. If the response to the request includes the string Hello! How are you today?, the test passes. If that string is not in the response, the test fails. The HTTP client then disconnects from the application.

In the import statements of this test class, you’ll notice that the test has some new dependencies. Before the test can be compiled by Maven, you need to update the pom.xml to include these dependencies.

The Apache commons-httpclient and junit-jupiter-engine dependencies are needed to compile and run the integration test EndpointIT class. The scope for each of the dependencies is set to test because the libraries are needed only during the Maven build and do not needed to be packaged with the application.

Now, the created WAR file contains the web application, and development mode can run any integration test classes that it finds. Integration test classes are classes with names that end in IT.

The directory structure of the project should now look like this:

    └── src
        ├── main
        │  └── java
        │  └── resources
        │  └── webapp
        │  └── liberty
        │         └── config
        └── test
            └── java

Running the tests

Since you started Open Liberty in dev mode, press the enter/return key to run the tests.

You see the following output:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running io.openliberty.guides.hello.it.EndpointIT
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.255 sec - in io.openliberty.guides.hello.it.EndpointIT

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

To see whether the test detects a failure, change the response string in the servlet src/main/java/io/openliberty/guides/hello/HelloServlet.java so that it doesn’t match the string that the test is looking for. Then re-run the tests and check that the test fails.

HelloServlet.java

 1// tag::comment[]
 2/*******************************************************************************
 3 * Copyright (c) 2017, 2019 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License v1.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-v10.html
 8 *
 9 * Contributors:
10 *     IBM Corporation - initial API and implementation
11 *******************************************************************************/
12 // end::comment[]
13package io.openliberty.guides.hello;
14
15import java.io.IOException;
16
17import javax.servlet.ServletException;
18import javax.servlet.annotation.WebServlet;
19import javax.servlet.http.HttpServlet;
20import javax.servlet.http.HttpServletRequest;
21import javax.servlet.http.HttpServletResponse;
22
23@WebServlet(urlPatterns="/servlet")
24public class HelloServlet extends HttpServlet {
25    private static final long serialVersionUID = 1L;
26
27    // tag::javadoc1[]
28    /**
29     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
30     */
31    // end::javadoc1[]
32    protected void doGet(HttpServletRequest request, HttpServletResponse response)
33        throws ServletException, IOException {
34        // tag::responseString[]
35        response.getWriter().append("Hello! How are you today?\n");
36        // end::responseString[]
37    }
38
39    // tag::javadoc2[]
40    /**
41     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
42     */
43    // end::javadoc2[]
44    protected void doPost(HttpServletRequest request, HttpServletResponse response)
45        throws ServletException, IOException {
46        doGet(request, response);
47    }
48}

When you are done checking out the service, exit development mode by pressing CTRL+C in the shell session where you ran the server, or by typing q and then pressing the enter/return key.

Great work! You’re done!

You built and tested a web application project with an Open Liberty server using Maven.

Guide Attribution

Building a web application with Maven by Open Liberty is licensed under CC BY-ND 4.0

Copied to clipboard
Copy code block
Copy file contents

Prerequisites:

Nice work! Where to next?

What did you think of this guide?

Extreme Dislike Dislike Like Extreme Like

What could make this guide better?

Raise an issue to share feedback

Create a pull request to contribute to this guide

Need help?

Ask a question on Stack Overflow

Like Open Liberty? Star our repo on GitHub.

Star