IntelliJ IDEA 2020.1 Help

Deploy a Java web application inside a Tomcat server container

You can use Docker to run a Tomcat server and deploy your Java web applications. This tutorial describes how to create a simple Java web application, build a deployable web application resource (WAR) file, and then deploy it inside a Tomcat server running as a Docker container.

Before you begin, make sure that Docker integration is properly configured.

Create a Java web application

  1. Open the File menu, point to New and click Project.

  2. Click Java, then select Web Application and click Next.

  3. Specify the name DockerJavaWebApp and click Finish.

  4. In the Project tool window, right-click src, point to New and click Servlet.

  5. Specify the name MyServlet and click OK.

  6. Open the src/ file and replace the default doGet() method with the following:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); try (PrintWriter writer = response.getWriter()) { writer.println("<!DOCTYPE html><html>"); writer.println("<head>"); writer.println("<meta charset=\"UTF-8\" />"); writer.println("<title> Servlet code!</title>"); writer.println("</head>"); writer.println("<body>"); writer.println("<h1>This is a simple java servlet.</h1>"); writer.println("</body>"); writer.println("</html>"); } }
    Add the necessary imports and libraries, if prompted to do so.

  7. Open the web/index.jsp file and paste the following HTML code as the entrypoint for the servlet:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Hello, I am a Java web app!</title> </head> <body> <h1>Simple Java Web App Demo</h1> <p>To invoke the java servlet click <a href="MyServlet">here</a></p> </body> </html>

  8. Open the web/WEB-INF/web.xml file and paste the following servlet configuration:

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="" xmlns:xsi="" xsi:schemaLocation="" version="3.1"> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping> </web-app>
    Make sure that the name of your servlet matches the case specified.

  9. Open the Build menu and click Rebuild Project to make sure that the servlet is compiling correctly. The Event Log should output Compilation completed successfully and you should see the compiled file out/production/DockerJavaWebApp/MyServlet.class.

Build a WAR artifact

  1. From the main menu, select File | Project Structure Ctrl+Shift+Alt+S and click Artifacts.

  2. Click Add the Add button, point to Web Application: Archive, and then click For 'DockerJavaWebApp:war exploded'. If necessary, create a manifest file.

  3. Open the Build menu and click Build Artifacts. Select to build the DockerJavaWebApp:war artifact. You should see the artifact out/artifacts/DockerJavaWebApp_war/DockerJavaWebApp_war.war.

Deploy your application inside a Tomcat server

  1. In the Docker tool window, right-click the Images node, and then click Pull image.

  2. In the Pull Image dialog, select the default Docker Hub registry and specify the Tomcat server image repository name and tag tomcat and latest.

  3. Click OK and wait until the image is pulled.

  4. In the Docker tool window, right-click the Tomcat server image and then click Create container.

  5. In the Create container popup, click Create.

  6. In the Create Docker Configuration dialog, do the following:

    • Specify the name of the container: TomcatContainer

    • Bind container port 8080 to host IP and port 8080

    • Map the WAR artifact output directory [PROJECT_PATH]/out/artifacts/DockerJavaWebApp_war/ to the Tomcat server deployment directory /usr/local/tomcat/webapps/.

    • Click Run to start the container.

    The Create Docker Configuration dialog
  7. When the container starts, open the following address in your web browser:

    You should see the following page:

    Simple Java Web App Demo start page

    Click the link to run the compiled Java servlet.

Last modified: 7 July 2020