I have a web application which I wish to configure via settings in an external folder (external to the container and to the .war file). Therefore I want to inject just a single setting into my webapp which is the root folder of my configurations. The reasons for doing this are so that the maintenance team can update configuration settings in nice plain text files without having to re-deploy the war file.
My question is, what is the best way to parametrize a web application in the case of just a single configuration setting. I know I can use a JVM arg and then detect it from my initialization servlet. Ideally, I'd like something that I can put in the server.xml (not the web.xml file) that can be programmatically acquired from my ServletContextListener.contextInitialized(ServletContextEvent paramServletContextEvent) method.
Is there a way to do this using the ServletContextListener approach or is another way?
We are using -Dconfig.location=/foo/bar/config.properties and it works fine. It's a JVM arg, so it goes to the startup script.
You can register properties via JNDI in server.xml, but I'm not convinced this is a better option. server.xml or catalina.sh - both are container-level
Related
I'm working on a Spring MVC project. When I run the application the URL is:
http://localhost:8080/insureYou/login
but I want:
http://localhost:8080/contextroot/insureYou/login
Is there any way of doing it without hardcoding?
In a spring-boot project you can set the context-root by specifying the following property in the application.properties file:
server.servlet.context-path=/yourcontextroot
Without spring-boot, it depends on the webserver and Tomcat offers a number of options.
I would personally opt for a META-INF/context.xml file in your war file containing the necessary information but you can also include the information in the server.xml file or in a ROOT.xml file.
See the following links for further guidance:
How to set the context path of a web application in Tomcat 7.0
https://tomcat.apache.org/tomcat-8.0-doc/config/context.html
https://www.baeldung.com/tomcat-root-application
This type of deployment however sometimes is handled separately, through an Apache server reverse-proxy or through URL rewriting.
I recommend you ascertain whether this type of need is already taken care of by your company's deployment procedures, as you may not need to deal with it at all.
Our Spring Boot application uses log4j2 for logging, but the admins of the server we are going to deploy it to require that:
the logging be fully configurable on their end, i.e., there must be a log4j2.xml file that they can edit to adjust logging formats, files, levels etc., and
The configuration must not be lost or overridden when a new version of the app is deployed (the upload war -> stop tomcat -> delete webapp folder -> start Tomcat) process will be automated.
Ideally, the path to log4j2.xml should be set in the webapps context .xml file — since that's where the DB connection config is. But I can't seem to find a way to make this work. All search results talk about using META-INF/web.xml or application.properties files (not an option, since they get overwritten on deploy), and the only SO question I could find (Spring application without web.xml log4j configuration) did not work for me ("No Configuration was provided") exception on startup.
Set the environment variable logging.config to be the location of your log4j2.xml file.
It's also recommended that you call it log4j2-spring.xml
See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html
section "Custom log configuration"
We build a webapp using ServletAPI 3 in combination with Tomcat 7 on RHEL.
I try to set a context specific init parameter [getServletConfig().getInitParameter("myinit");] via any of
${CATALINA_HOME}/server.xml (no conf-directory in between but obviuosly the same)
{engine}/{host}/{app}.xml
and to provide some meaningfull default ressource values via webapps war file content "META-INF/context.xml" in parallel.
But as soon as I define a context definition in the XMLs the defined DB connection we provide by context.xml within the war is ignored.
We build the webapp with ant as a single {app}.war file.
Obviously I don't provide the right settings but I don't understand how to do that without moving the db connection settings from META-INF/cotext.xml to the containers context definition (we don't want to do that - though this may obviously be a viable alternative).
Is it possible at all? If so: How? Is there an alternative option to do something similar?
Thanks a lot in advance!
Dear fellow Developers,
I am working on improving my Java Web Services, and I am trying to use a more delicate way of getting the directory path of properties files in a Java Web Service.
In order to make my Java Web Application easier to be deployed on an Apache Tomcat Server, I add the following line to the web.xml file:
<env-entry>
<env-entry-name>loggerPropertyFile</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>/Some/Long/Directory/File/Path/Which/May/Change/conf/LoggerInfo.properties</env-entry-value>
</env-entry>
As the above xml code depicts, I have placed a Properties file somewhere in the local Filesystem, and I want my Web Service to initialize its logger class, based on that configuration. As you can realize this path changes every time I deploy my web service to another server.
Thus, I figured out that I may be able to use the $CATALINA_BASE property, in order to make the environment entry path smaller. How can I retrieve the CATALINA_BASE value from inside my Java Web Service's Code (how is done on Linux and how is done on Windows)??
Thank you.
try System.getProperty("catalina.base");
I have two war files WARA and WARB deployed in a tomcat, both use same log4j properties file. How to reset the log4j configurations in both war files through another stand alone application deployed in same tomcat without disturbing WARA and WARB? Is it possible to do?
If polling is an option you can use PropertyConfigurator.configureAndWatch().
More here.
If you need to do it manually the process is bit longer. On a high level its as follows.
Configure JMX for your application server / servlet container.
Write an MBean to trigger full reconfiguration.
Register the MBean in WARA and WARB.
Connect to the MBean server from the other webapp.
Invoke the MBean in WARA and WARB.
If you can provide more details on what software stack your are using I may be able to provide specific details on how to do the above.
not the best way , but make a jar file that contains properties file ,and place it tomcat lib folder ,all applications use same propertyfiles.