Using ServletContext.setAttribute() you can place objects of any type inside the ServletContext object. Is there a way to do this in the context.xml file? As far as I know you can only place primitive data types inside the “context-param” tag of context.xml.
Why do I wan’t to do this?
I have several servlets that I would like to access the same instance of an object that is suppose to abstract access to a particular table in a database, sort of like an entity bean. It’s not absolutely necessary to do it this way but I wanted to explore the ServletContext boundaries. I’m using Apache Tomcat 7.
The answer is no. Only primitive data types can be declared inside the XML file. It’s documented here -> http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Context_Parameters
Spring MVC makes this unnecessary as well, but I wanted to avoid using a heavy weight DI framework for something this simple.