Home > Blockchain >  Removing "path" attribute from Context element breaks context.xml file
Removing "path" attribute from Context element breaks context.xml file

Time:05-23

I am running a Java web application from NetBeans using Tomcat as the Servlet container.

I previously had a Context element in the META-INF folder (META-INF/context.xml) which defined the path attribute as empty:

<Context path="">
...
</Context>

(This is my context for in-place deployment from C:[projectname]\build\web when running on my local machine from NetBeans).

The app is run on my local machine by right-clicking on the project and slecting "run". From inspecting the files, I think that NetBeans is creating a build folder and placing all compiled files in it and this is where my project is run from (as opposed to when I deploy on my production environment and simply drop a war file in the webapps folder). (this is what I think is happening - not sure).

After upgrading Tomcat (from 8.0.43 to 10.0.21), a warning started showing up:

The path attribute with value [] in deployment descriptor [...] has been ignored

So I removed the path attribute from the Context element.

However, this caused my build to fail:

Cannot deploy the module. The context.xml file seems to be broken. Check whether it is well-formed and valid.

How can I fix this? What am I not understanding?

CodePudding user response:

Summary

I am able to recreate your "failed deployment" problem. It is specifically related to how NetBeans integrates with Tomcat, when managing Tomcat projects directly from within NetBeans.

Although the warning you mentioned can be ignored (when the deployment succeeds), it's worth taking a closer look at how NetBeans manages Tomcat deployments. Also I think the error message is a bit misleading. In your case, it is not due to a broken/invalid XML file, but rather to a missing XML file.

As you note in the question:

NetBeans is creating a build folder and placing all compiled files in it and this is where my project is run from

And yes this is different from when you drop a WAR into the webapps folder, when you run your application outside of NetBeans (e.g. in Production).


Details

Let's first assume your application is called "TomcatBasicDemo" and this is the name you want to use for your application's root context - for example:

http://localhost:8080/TomcatBasicDemo/

(We will look at the path="" example later).

NetBeans finds your app's context name in the context.xml file in your application's META-INF directory - for example:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/TomcatBasicDemo"/>

NetBeans uses this path value (TomcatBasicDemo) to create a file in the Tomcat server you are using for your web project - for example:

C:\tomcat\apache-tomcat-10.0.21\conf\Catalina\localhost\TomcatBasicDemo.xml

As you can see, the name of the file is based on the path from your context.xml file.

Inside this new TomcatBasicDemo.xml file you will see something like the following:

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:\NetBeansProjects\TomcatBasicDemo\target\TomcatBasicDemo" path="/TomcatBasicDemo"/>

That docBase path is the directory you mention in your question - the "build" directory created by NetBeans.

This is the mechanism which NetBeans uses to "deploy to Tomcat" instead of dropping a WAR file into the webapps directory. It allows NetBeans to keep its code in its own project structure, separate & externalfrom Tomcat.

(I assume this also probably simplifies code debugging from within NetBeans.)


What About the Build Failure?

Now you get that warning:

The path attribute with value [] in deployment descriptor [...] has been ignored

If you try to handle that warning by editing your application's META-INF/context.xml file and removing the path="/TomcatBasicDemo" attribute, then NetBeans will no longer be able to create the XML file in Tomcat's conf\Catalina\localhost directory because it will not know what the file needs to be called.

This is why Tomcat "Cannot deploy the module.". The XML file it needs is missing.


What about path=""?

Coming to the specific example in your question, if your application's META-INF/context.xml file contains the following:

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

...then that is valid - since the empty string defaults to a file called ROOT.xml in Tomcat's conf/Catalina/localhost directory.


What about the Warning?

You can safely ignore it. It is purely for informational purposes.

I don't know of any way to prevent NetBeans from including that path parameter when it creates the TomcatBasicDemo.xml file - or the ROOT.xml file (in your case, for path="").

There are more details provided in the Tomcat configuration documentation for the context container - including a table showing examples of how these XML file names are derived: contexts and naming guidelines.

I was not able to find an obvious changelog entry for when this warning was introduced.

CodePudding user response:

From tomcat docs

This attribute must only be used when statically defining a Context in server.xml. In all other circumstances, the path will be inferred from the filenames used for either the .xml context file or the docBase.

If context file is named myapp.xml then the app will appear under /myapp and the warning can be safely ignored.

As the error posted by the OP, the xml file must be invalid itself regardless of tomcat.

As a test on tomcat 9, create a context file as below under /usr/share/tomcat/conf/Catalina/localhost/myapp.xml and start tomcat

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

The log shows an error because /srv/tomcat/webapps/myapp does not exist but other apps are started anyway and there's no error about malformed XML files

21-May-2022 12:36:21.946 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying deployment descriptor [/var/cache/tomcat/Catalina/localhost/myapp.xml]
21-May-2022 12:36:21.950 SEVERE [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDescriptor Error deploying deployment descriptor [/var/cache/tomcat/Catalina/localhost/myapp.xml]
        java.lang.IllegalStateException: Error starting child
                at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:720)

        Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@2a4c8ee]
                at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)

                ... 24 more
        Caused by: java.lang.IllegalArgumentException: The main resource set specified [/srv/tomcat/webapps/myapp] is not valid
                at org.apache.catalina.webresources.StandardRoot.createMainResourceSet(StandardRoot.java:751)
                at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:708)
                at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
                ... 28 more
21-May-2022 12:36:21.952 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of deployment descriptor [/var/cache/tomcat/Catalina/localhost/myapp.xml] has finished in [6] ms
  • Related