Do i need a context.xml file to deploy spring webapp to tomcat - spring

I am new to Tomcat, so I apologize if this is a dumb question. I have created a spring mvc webapp that currently runs locally using the maven-jetty-plugin.
I can successfully create the WAR file. I would like to deploy the WAR file into a tomcat6 instance. However, I am not sure if I need to create a context.xml file for tomcat? And if I do, where would I place the file in my spring webapp? My current directory structure looks like this:
src
|
|-main
| |-java
|-resource
| |-META-INF
|-webapp
| |-WEB-INF
|-web.xml

The context.xml file is used to configure application specific instructions for your container (tomcat). For instance, you can define JNDI resources, loggers, valves, etc. See Context Container for more details.
By default, Tomcat will auto-generate a default context.xml for your war if you do not specify one yourself and store it within its own configuration files in /conf/[enginename]/[hostname]/[context-root].xml
If you want to include it as part of your war, you can place it in /META-INF/context.xml within your war.

No, you don't. The context.xml file is optional, and by no means required by Spring.
context.xml is used to configure Tomcat itself. The defaults are sensible, there's no need to override them unless you have a good reason.

Related

Using jasypt-spring-boot when deploying to Apache Tomcat

I'm trying to use the jasypt-spring-boot and deploy it to a Tomcat server as war.
How to pass the encryptor password, in this case, to ensure the encrypted values could be read?
All the provided example are about running a jar file or a Spring Boot app as follows:
java -Djasypt.encryptor.password={my-password-to-decrypt} -jar target/jasypt-spring-boot-demo-0.0.1-SNAPSHOT.jar
May be add some settings to catalina.properties file in the Tomcat conf folder as we do for example when defining active profile?
I figured out how to achieve that:
create a setenv.sh file in CATALINE_HOME/bin folder
add the following entry to set the environment variable on the Tomcat startup:
export JASYPT_ENCRYPTOR_PASSWORD=your-password
save and restart Tomcat.

Read Spring properties in React

I have a Spring/React webapp. In my application.properties file I defined spring.data.rest.base-path = /apiso when running the app locally, everything is accessible on localhost:8080/api. If I deploy this to my tomcat, the all the stuff goes to localhost:8080/warname/api.
I can easily define my warname in my properties file. And in React,
path: '/api/myStuff'
I can access my data.
Also I can change that to
path: '/warname/api/myStuff'
and everything will work. But to make things easier, it would be better to read the warname from my pom.xml so I wouldn't have to change every path in my .js. How to get that done?
If you have a directory named ROOT in your Tomcat directory, you have to remove it and change the name of your war to ROOT.war so that when Tomcat explodes the war it will be the main root project.

How can I use an properties file inside JBOSS AS7 in Spring?

I want to use somthing like this:
//#PropertySource("classpath:myconfig.properties")
My config file is in the deployments folder.
Remember:JBOSS AS7!!
In my case (in JB 7), I have the properties in an exploded war file under WEB-INF and they get picked up. I assume that you meant that the war file/folder was in the deployments folder.
and I use Spring 4

how to set java class loader PARENT_LAST

i have a spring mvc web application that I need to change the class loader on. I need to change the class loader to be equal to PARENT_LAST. I am using WAS 6.1 and already have a jacl script from a previous web application I can copy to do the job.
In the last application Apache ant was used and what they did was to make the deploy dependent on running the jacl script.
In my new web application I am using maven install to create a war file and am deploying that war file to my application server.
How can I set the class loader to be PARENT_LAST using maven? I know how to do it in the console but if there was a way to do it using scripting that would be nice.
Also will this setting be placed somewhere in the war file so that on deploy of the application the setting will be picked up. This question comes from my lack of understanding of how jacl scripts work?
thanks
If you are only deploying the WAR file itself you can't control this, but if you have your WAR file in an EAR file you can use the deployment.xml solution. The deployment.xml file would look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<appdeployment:Deployment xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:appdeployment="http://www.ibm.com/websphere/appserver/schemas/5.0/appdeployment.xmi" xmi:id="Deployment_1347529484613">
<deployedObject xmi:type="appdeployment:ApplicationDeployment" xmi:id="ApplicationDeployment_1347544766353" startingWeight="99" warClassLoaderPolicy="SINGLE">
<modules xmi:type="appdeployment:WebModuleDeployment" xmi:id="WebModuleDeployment_1347543866613" startingWeight="1" uri="YourWebApp.war" classloaderMode="PARENT_LAST"/>
<classloader xmi:id="Classloader_1347543866613" mode="PARENT_LAST"/>
</deployedObject>
</appdeployment:Deployment>
Once you are done all you need to do is to add the file in the correct location of your EAR project build assuming you are using src/main/application that would be src/main/application/META-INF/ibmconfig/cells/defaultCell/applications/defaultApp/deployments/defaultApp/deployment.xml and build the EAR using Maven as normal.
During server deployment this will be picked up by WAS.
AFAIK there is no way to preconfigure WAR for PARENT_LAST during assembly. Classloading policy is specified during deployment, thus the way of setting it depends on how application is deployed.
Changing the policy using the script is straightforward. Scripts are run using wsadmin tool. The Jython snippet below does the job. It can easily be converted to Jacl.
dep = AdminConfig.getid('/Deployment:app_name/')
depObject = AdminConfig.showAttribute(dep, 'deployedObject')
classldr = AdminConfig.showAttribute(depObject, 'classloader')
AdminConfig.modify(classldr, [['mode', 'PARENT_LAST']])
AdminConfig.save()
Websphere uses deployment.xml file to govern deployment setting of each module in an ear file. You can change the classloader setting in deployment.xml at the following path:
/MyTestEAR/META-INF/ibmconfig/cells/defaultCell/applications/defaultApp/deployments/defaultApp/deployment.xml
I do not know how you can configure that in Maven.

Custom tomcat context.xml for Spring App

I have a Spring App, and I would like change the session cookie path. I know this can be done in the server's context.xml file, but I don't have permission to edit that file on my server. Is there another way to do this, maybe by adding another context.xml file somewhere?
Btw, I added a context.xml file to my project's META-INF folder, but it didn't work.
Bundle your context.xml with your war file.

Resources