Skip to content
Advertisement

How to get a context.xml working with Tomcat and IntelliJ IDEA

I’ve been learning about connection pools, and I have gotten one working by using Tomcat’s PoolProperties. Now I would like to set one up using a context.xml file with Tomcat and IntelliJ, but I can’t get this to work. How is this done?

A new project with a 4.0 Web Application framework can automatically create a web/WEB-INF directory with a web.xml file, but the web/META-INF directory containing a context.xml file is not created. If I do create web/META-INF/context.xml myself through the Tool Window > Project, and I use this to set up a connection pool as shown below, the file seems to be ignored.

This is my guess as to what might be happening, though I’m sure this is filled with errors: A WAR file is a packaged directory that is sent to Tomcat in an arrangement that Tomcat understands. web/WEB-INF/web.xml is a required file that is needed for a variety of reasons, one of which is to set up servlets. web/META-INF/context.xml is optional, and thus by default, IntelliJ does not create it. When a web/META-INF/context.xml is created manually, it must also be included into the WAR file otherwise IntelliJ will not send it to Tomcat.

Even if the previous paragraph is true though, I still have not gotten Tomcat to use context.xml after trying to add it to the WAR. Maybe I didn’t add it correctly.

context.xml

JavaScript

index.jsp

JavaScript

Advertisement

Answer

I had the same issue and adding context lookup to my java code to use the resource defined in context.xml did the job.

It’s the accepted solution from: Tomcat and JDBC connection pooling

Still works with Tomcat 9.0.39

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement