Home > front end >  .war on tomcat on docker, 404 on servlet
.war on tomcat on docker, 404 on servlet

Time:11-03

I finally found the motivation to work with Docker : I tried to deploy a basic "hello-world" servlet, on a tomcat running on a docker container.

This servlet works perfectly when I run it on the Tomcat started by intelliJ.

But when I use it with Docker, using this Dockerfile

FROM tomcat:latest
ADD example.war /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]

And I build/start the image/container:

docker build -t example .

docker run -p 8090:8080 example

The index.jsp is displayed correctly at localhost:8090/example/, but I get a 404 when trying to access the servlet at localhost:8090/example/hello-servlet

At the same time, I can access localhost:8080/example/hello-servlet, when my non dockerized tomcat runs, and it works well.

Here is the servlet code :

package io.bananahammock.bananahammock_backend;

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h1>"   message   "</h1>");
        out.println("</body></html>");
    }

    public void destroy() {
    }
}

What am I missing?

CodePudding user response:

Since August 31, 2021 (this commit) the Docker image tomcat:latest uses Tomcat 10 (see the list of available tags).

As you are probably aware, software which uses the javax.* namespace does not work on Jakarta EE 9 servers such as Tomcat 10 (see e.g. this question). Therefore:

  • if it is a new project, migrate to the jakarta.* namespace and test everything on Tomcat 10 or higher,
  • if it is a legacy project, use another Docker image, e.g. the tomcat:9 tag.
  • Related