mavenservletsweb-applicationsjava-11tomcat9

Can't set context path to / at running web application from Dockerfile


I created just the minimum for a web application (for learning purposes) but I can't set the context path when I run it from a Dockerfile image (but it works just fine when I don't use it this way). In my multi-stage Dockerfile I build the application then I use it under tomcat. This is the line for that purpose:

COPY --from=builder /app/target/*.war $CATALINA_HOME/webapps/

This is the code of my servlet:

package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/firstTest")
public class FirstServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");//setting the content type
        PrintWriter pw=response.getWriter();//get the stream to write the data
        //writing html in the stream
        pw.println("<html>");
        pw.println("<body>");
        pw.println("Welcome to servlet");
        pw.println("</body>");
        pw.println("</html>");
        pw.close();//closing the stream
    }
}

My project name is servletjdbc so it makes a war and a map with the version: servletjdbc-1.0-SNAPSHOT. I can use http://localhost:8080/servletjdbc-1.0-SNAPSHOT/ and http://localhost:8080/servletjdbc-1.0-SNAPSHOT/firstTest

But I would like to use http://localhost:8080 and http://localhost:8080/firstTest

I tried to put in my web.xml something like this:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
</context-param>

but I got the message:

Element param-name is not allowed here Element param-value is not allowed here

My web.xml is this now:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="4.0" 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_4_0.xsd">

</web-app>

What could be my mistake here?

If I can provide any kind of necessary additional information, please let me know.

Thank you very much for your help in advance.


Solution

  • If you want to deploy your application to the / context then you should rename the war file to ROOT.war. It is a simple way to deploy to the root application context.

    Another way, which is used by IntelliJ IDEA is to change a server.xml file for Tomcat. There's my answer here.

    The <context-param> is useless if you want to set a context path to root. The error is that you place it in the wrong place or have incorrect DTD/XSD reference. If you want to read a detailed description about it then you should read this.