0

This Spring example uses a Java configuration to register a servlet. To test this,

  1. add a latest maven-war-plugin to ignore missing web.xml, and
<build>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.2</version>
        </plugin>
    </plugins>
</build>
  1. build a war, and
  2. create a Dockerfile to use Tomcat, and
FROM tomcat

COPY example-05-dispatcher-servlet-code-configuration-2-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/
  1. build and run a container, and
docker build .
docker run -p 8080:8080 -it <image>
  1. send a request to /hello.
curl -4 -v -XGET http://localhost:8080/hello

However, the tomcat didn't find the controller, and this was what I got:

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

What did I wrong, and how can I fix it?

b1sub
  • 2,268
  • 1
  • 12
  • 30
  • Tomcat didn't find the servlet context (application), because its prefix is `/example-05-dispatcher-servlet-code-configuration-2-1.0-SNAPSHOT/`, not `/`. Copy the file as `ROOT.war` to deploy your application at the root of your server. – Piotr P. Karwasz Jan 22 '22 at 21:16
  • 1
    Basically `tomcat` refers to Tomcat 10 from some time now. Use the `tomcat:9` tag instead. – Piotr P. Karwasz Jan 22 '22 at 21:39
  • @PiotrP.Karwasz It did work. Thanks! – b1sub Jan 22 '22 at 21:54

1 Answers1

1

My guess is that you miss the context path of the app which is the name of the folder inside /usr/local/tomcat/webapps/ that containing the exploded WAR . So in your case , please try :

curl -4 -v -XGET http://localhost:8080/example-05-dispatcher-servlet-code-configuration-2-1.0-SNAPSHOT/hello

If you really want to access by http://localhost:8080/hello , you have to deploy the WAR to /usr/local/tomcat/webapps/ROOT/. As the tomcat docker images already has a welcome page app deployed to this context , you need to delete this app first and rename your WAR to ROOT.war , something like :

FROM tomcat
RUN rm -rvf /usr/local/tomcat/webapps/ROOT
COPY example-05-dispatcher-servlet-code-configuration-2-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/ROOT.war
Ken Chan
  • 75,099
  • 24
  • 129
  • 153
  • Thanks for your time! Unfortunately, `curl -4 -v -XGET http://localhost:8080/example-05-dispatcher-servlet-code-configuration-2-1.0-SNAPSHOT/hello` also gives me `404 NOT FOUND`. This is driving me crazy, haha. – b1sub Jan 22 '22 at 21:29