do we need to deploy war after changing property file - java

Do we need to deploy war after changing the property file in java?

At least you may have to restart the application so that it reads the new property file. So re-deploying the entire application may not be necessary.

you do need war to deploy if property file is not loaded each time it is needed

Yes, unless you make your context reloadable. To do that update your conf/context.xml, adding the "reloadable" attribute to the root node like so:
<Context reloadable="true">
Take a look at the Tomcat Config Docs.

Related

How to set up Tomcat to use the config file in conf/Catalina/localhost?

so my question is how can I set up a Tomcat Server to use this configuration file conf/Catalina/localhost/MyApp.xml?
It works like a charm if my application is named like this: MyApp.war so tomcat will extract the archive to MyApp.
But I want to use a name with the version inside like MyApp##1.0-SNAPSHOT.war.
Is it possible to configure the tomcat so it will use the MyApp.xml anyway?
You can set path and docBase attributes of Context element as described in the docs.
Create a directory outside $CATALINA_HOME/webapps and place war files there, let's say /opt/tomcat/wars/. Then set docBaseattribute to the full path to your war. Make user tomcat user has read permissions on that directory. If war is inside webapps directory you could end up with a double deployment depending on deployOnStartup and autoDeploy attributes of Host element.
<Context path="/MyApp" docBase="/opt/tomcat/wars/MyApp##1.0-SNAPSHOT.war">
...
</Context>
You can try also naming the xml the same as the war file.

Where is Spring MVC context path set?

I am not sure how context path is set.
When I rename my .war file in tomcat on autodeploy, the web page goes to localhost:8080/newDirectory as expected, however for some reason wherever there's a call to pageContext.request.contextPath in a Spring based page, it still returns the old context path.
I tried to override the context path by setting:
<context path="/newDirectory" docBase="appName" override="true"></context>
in server.xml but it doesn't work.
My question is, where does Spring read its context path from? I used Maven and I did see there's a
<appContext>/${project.artifactId}</appContext>
in the pom.xml, does this mean I need to rename the artifactId to newDirectory ?
I have also tried adding that <context path="/newDirectory"...> in /META-INF/context.xml (which now I know will be ignored anyway due to my server.xml changes).
Thanks in advance for your answer.
It not depends on Spring maybe you are using a maven plugin to build your war that reads the appContext property. You can read about definig ServletContext in this thread.
Every PageRequest will get current HttpServletRequest object and get context path of current request and append your .jsp (that will work even if the context-path this resource is accessed at changes).
Eg,
if you have war file as MyCompany.war and having a page with ${pageContext.request.contextPath}/MyJspPage.jsp.
Then your context path is http://abc/MyCompany and it works as http://abc/MyCompany/MyJspPage.jsp.
Suppose if you change your war file as OurCompany.war,
then your context path changes to http://abc/OurCompany and Jsp will work as http://abc/OurCompany/MyJspPage.jsp.
It means context path will change automatically to name of the application(War file name) with out any changes.
In your case,after renaming war file name with newDirectory,your webserver will deploy newDirectory application but still newDirectory application exist in the web server.I think you should delete old application from webapp and then check by reloading your newDirectory application.

In Tomcat 7 do you need to copy context.xml into conf/Catalina/locahost for it to take effect

Just moved from Tomcat 6 to Tomcat 7 and noticed that when you deploy a webapp called widget the META-INF/context.xml no longer get copied to conf/Catalina/localhost/widget.xml.
What Im unclear about is whether it needs to be, will the settings in context.xml be used if left in META-INF or do they only have effect if moved to conf/Catalina/localhost/widget.xml
Im using a vanilla tomcat 7 installation
See the documentation (my emphasis):
Individual Context elements may be explicitly defined:
In an individual file at /META-INF/context.xml inside the application
files. Optionally (based on the Host's copyXML attribute) this may be
copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to
application's base file name plus a ".xml" extension.
The documentation for Host has this to say on copyXML:
Set to true if you want a context XML descriptor embedded inside the
application (located at /META-INF/context.xml) to be copied to xmlBase
when the application is deployed. On subsequent starts, the copied
context XML descriptor will be used in preference to any context XML
descriptor embedded inside the application even if the descriptor
embedded inside the application is more recent. The flag's value
defaults to false. Note if deployXML is false, this attribute will
have no effect.
As I understand it, the context.xml in META-INF is used unless the element deployXML is set to false. Note from the Tomcat 7 docs, the default is true unless a security manager is enabled!
However, from the docs, I was under the impression that setting the copyXML attribute in the context.xml itself (without touching server.xml) would cause the file to be copied to conf:
<Context antiJARLocking="true" path="/widget" copyXML="true" />
However, using Tomcat 7, it seems that the file doesn't get copied to conf unless the copyXML is set to true on the as Frank answered above.

customize web application root context in apache

I have web application abc.war and I want to deploy it on Apache Tomcat.
The problem is that, by default, the path to this application is http://<server-name>/abc
but I want to access it as http://<server-name>/xyz.
I put into WAR's META-INF folder the file context.xml that is :
<Context path="/xyz" docBase="abc" override="true" />
The application WAR abc.war is located under %CATALINA_HOME%\webapps and it is extracted to %CATALINA_HOME%\webapps\abc folder.
Also, while deployment, the file context.xml from abc/META-INF is copied to %CATALINA_HOME%\conf\Catalina\localhost as abc.xml
It seems that this should work, but I still can't access my application through http://<server-name>/xyz, but only through http://<server-name>/abc
In addition, I still see in apache log the following line while deployment of abc.war :
context path = /abc
Could anybody, please, help while this doesn't work, or tell if there is any way of deploying of web application on apache such that application could be accessed by customized path (that does not relate to war-file name) ?
Thanks in advance.
Take a look at the docs:
The context path of this web application, which is matched against the beginning of each request URI to select the appropriate web application for processing. All of the context paths within a particular Host must be unique. If you specify a context path of an empty string (""), you are defining the default web application for this Host, which will process all requests not assigned to other Contexts.
The value of this field must not be set except when statically defining a Context in server.xml, as it will be inferred from the filenames used for either the .xml context file or the docBase.

How can I set hibernate database config from outside config file?

I need to have hibernate database config set from outside text file, how can I do it? Is there some kind of method for this, or do I have to make my own?
You can programaticaly configure hibernate within your app: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-programmatic
HTH
Hibernate configuration file (*.cfg.xml) have to be in the class path of your application's war.
But, you can maintain file like 'install.properties' in deployment folder of your application.
Inside it you can maintain database related properties.For eg.
database.name =
database.hostname=
database.username=
databse.password=
Use some predefined constant string inside 'hibernate.cfg.xml' that you can replace during time of application deployment task. For eg.
<property name="hibernate.connection.url">jdbc:mysql://$database.hostname/$database.name</property>
you can write an 'ant' task which 'unwar' the war then replace constant strings with values as mentioned inside 'install.properties' and then make 'war' again.
In this way you can separate out configuration settings from application code structure.
Hibernate configuration files have to be on the class path but can be outside your war. The exact way to add a directory or files to the class path will depend on your app server so you'll have to mention which one you're using (for Tomcat, see this previous question for example).

Categories

Resources