I use the virtual directory to serve the static resource uploaded by user. So I don't need to backup these files when redeploying the app.
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context path="/upload" docBase="/Users/junyu/Web/upload/"/>
</Host>
I wrote these code into server/server.xml,and could be detected by gretty buildProduct task。Here is the output directory:
directory
Running output:
output
The upload context start successfully, but I couldn't access the pics in the docBase. 404 error.
Abandon the method using server.xml to config context.
Just config in the output/hello-web/server.json as following:
"webApps": [
{ "contextPath": "/hello-web", "resourceBase": "webapps/hello-web.war" },
{ "contextPath": "/upload", "resourceBase": "/Users/junyu/Web/upload/" }
]
The upload context will work, and pics in docBase could be accessed.
The question is server.json be newly produced in every build, and I have to change the server.json again. Is there a way to config once for all.
And, 'Web-app extra resource bases' just copy the resources into the project, and won't change after the original resource files change.
Related
I do have a Spring Boot Application which uses custom context.xml for the tomcat.
The context.xml contains property, defining the spring active profile
<Context>
<Environment name="spring.profiles.active" value="profileName" type="java.lang.String" override="false" />
</Context>
File location is the /src/main/webapps/META-INF
I was expecting that after the file is deployed to the tomcat, context xml will be automatically picked by tomcat and thrown to the conf/catalina/localhost/
As it turned out, the war was deployed, but the conf/catalina/localhost stayed empty.
After reading the docs i've found out that the server.xml has to be updated with the copyXML parameter as Host container.
The documentation says:
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.
My Host looks like
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true" copyXML="true">
</Host>
After restarting the server and redeploying the app, the /conf/Catalina/localhost still stayed empty.
Do you have any suggestions, what actions have to be taken in order to use custom context.xml?
I've figured it out.
My build script did not copy the file correctly, the file name was not "context.xml" but "appName.xml" and tomcat didn't pick it up.
/...too long and unclear explanation.../
In other way, I search the correct way to reach the site with just (one or more) domain name (then www.my_domain.it and not www.my_domain.it/appname), and all of these must point to the same instance of the webapp (so that from any domain name you access, the same Runtime data will always be displayed).
So, yes I have deployed the WAR file in tomcat/webapps folder, now, the correct to edit the conf/server.xml to get the result mentioned above, corresponds to one of the following?
1.
<Host name="my_domain.eu" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Alias>www.my_domain.eu</Alias>
<Alias>www.my_domain.it</Alias>
<Alias>www.my_domain_2.it</Alias>
<Context path="" docBase="appname" debug="0" privileged="true" />
...
</Host>
2.
<Host name="my_domain.eu" appBase="webapps/appname" unpackWARs="true" autoDeploy="true">
<Alias>www.my_domain.eu</Alias>
<Alias>www.my_domain.it</Alias>
<Alias>www.my_domain_2.it</Alias>
<Context path="" docBase="appname" debug="0" privileged="true" />
...
</Host>
3.
<Host name="my_domain.eu" appBase="webapps/appname" unpackWARs="true" autoDeploy="true">
<Alias>www.my_domain.eu</Alias>
<Alias>www.my_domain.it</Alias>
<Alias>www.my_domain_2.it</Alias>
<Context path="/appname" docBase="appname" debug="0" privileged="true" />
...
</Host>
4.
<Host name="my_domain.eu" appBase="webapps/appname" unpackWARs="true" autoDeploy="true">
<Alias>www.my_domain.eu</Alias>
<Alias>www.my_domain.it</Alias>
<Alias>www.my_domain_2.it</Alias>
<!-- without context definition -->
...
</Host>
Otherwise, what is the correct solution?
Thanks to all!
I'm assuming that you have one of the instances of your app in the webapps directory, from where it's automatically deployed under its name. With the Context element, you can deploy applications from anywhere in the filesystem.
However, it's best practice to not edit server.xml with this information, but rather create individual context.xml files in conf/hostname, or just deploy to the host's webapps directory. Anything that you're configuring in server.xml requires a server restart if you want to change anything. Configuration outside of server.xml can be picked up at runtime, without restart.
Edit: Probably the takeaway of my previous answer wasn't clear:
When you use Context elements to configure: I'm proposing not to use the webapps directory for deploying your webapps. Just point to the directory where you actually deployed your webapp. This will make sure that no context is deploying your app from webapps/appname as /appname, even though your context definition points to /.
An alternative is to deploy your application with the name ROOT in webapps - this is a shortcut to actually deploy this webapp as / (careful: ALL CAPS for the directory- or file-name (ROOT.war) even on Windows if I remember correctly). But IMHO it would be clearer if you decide for either Context definition to determine the path (but deploy outside of appbase (webapps), or deployment in appbase (e.g. webapps) deployment.
Mocking around with appbase to point to some directory within webapps will make sure that you irritate anyone who is expecting that applications in webapps directory will deploy to Tomcat. That's an easy way to sink a lot of time into debugging phantom problems - don't do that.
I currently have an apache tomcat application running on my machine. I need to make this application accessible over an external URL.
I can access the application locally at http://localhost/c. I want to be able to access the application externally at https://example.com/b/001/c. TO achieve this, I made the below change to my conf/server.xml file
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="" path="/b/001" reloadable="true" />
After making this change, my local set up works fine when I test with the below URL
http://localhost/c/1.jsp (works fine)
When I try to load the external URL (https://example.com/b/001/c/1.jsp) however, I get the below error
An error occurred at line: [15] in the generated java file: [......./1_jsp.java]
Only a type can be imported. a.c.d.e.database.DB resolves to a package
What change should I make to make this publicly available on the external server?
How can I deploy mywebapp-1.0.0.war to $TOMCAT_HOME/webapps directory with context path /mywebapp using context.xml inside the war file on Tomcat 8?
I'm getting back to work with Tomcat after long time since version 5. I'm used to create META-INF/context.xml inside my war file:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/mywebapp">
...
</Context>
Maven creates a war file with this name: mywebapp-1.0.0.war
But when I deploy the war file to $TOMCAT_HOME/webapps directory the context path will be http://localhost:8080/mywebapp-1.0.0 instead of http://localhost:8080/mywebapp/.
Also I see that $TOMCAT_HOME/conf/Catalina/localhost is empty, instead of having the xml file copied from the war file deployed.
I also added to $TOMCAT_HOME/conf/server.xml the deployXML="true"
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
deployXML="true">
It is not possible to put a war inside the webapp directory and set the path attribute in META-INF/context.xml file at the same time. Tomcat 8 documentation, clearly says about this attribute:
This attribute must only be used when statically defining a Context in server.xml. In all other circumstances, the path will be inferred from the filenames used for either the .xml context file or the docBase.
Can't you just rename your war file to mywebapp (via Maven or else) so that Tomcat will deploy it under /mywebapp ?
Thanks for your research j2gl!
I've found out that good way how to achieve both .war file with full name with version and deployed short path is to use Tomcat manager API. For example through Tomcat7-maven-plugin.
I am using tomcat 7.
When I set context different from war file name, everything works fine.
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="../webapps/abc.war" path="/def" reloadable="true" />
</Host>
But at tomcat startup I see two exploded folder abc and def.
Please help if anybody knows about this issue resolution.
Thanks.
From the Tomcat doc (see "path" attribute):
Even when statically defining a Context in server.xml, [the path] attribute
must not be set unless either the docBase is not located under the
Host's appBase or both deployOnStartup and autoDeploy are false. If
this rule is not followed, double deployment is likely to result.
This is a bit obscur, but my understanding is that you need to use an xml context file to achieve what you are trying to do. How to define a context config file is documented in the above link.
A simpler fix would be to just rename your war file to def.war.