1

I have a project using Jakarta EE and I'm trying to set a contextual path, but I can't do it. When setting the context path in the web.xml, everything works the same as without it. If you try to get it through ServletContext.getContextPath(), then outputs an empty string. I attach screenshots web.xml file, jsp file and project structure.web config, jsp, structure

1 Answers1

1

According to the documentation:

A context root identifies a web application in a Java EE server. A context root must start with a forward slash (/) and end with a string.

Which means the root context path is determined by the application server when the web application is deployed. If you do not deploy a WAR file but rather try to run the project using the embed server(tomcat etc), you can not directly set the root context path of the application in the web.xml file. Rather set it in either application.properties or application.yml file like this

server.servlet.context-path=/my-app

When you are deploying a WAR file in servers like GlassFish or JBoss, you have to add server-specific files in the WEB-INF folder.

  • For JBoss add jboss-web.xml
  • For GlassFish add sun-web.xml

Then in the jboss-web.xml file add the following configuration

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <context-root>/my-app</context-root>
</jboss-web>

Now you can access API by appending context root at the beginning.

user207421
  • 305,947
  • 44
  • 307
  • 483
mahfuj asif
  • 1,691
  • 1
  • 11
  • 32