Tomcat issues with Host, Contexts and webapp double instances - java

/...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.

Related

Deploying War. Tomcat not taking context.xml from META-INF in consideration

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.

Tomcat context's docBase won't work in gradle gretty

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.

Context path configuration in Apache tomcat 8.0.30

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?

issue with tomcat context setting in server.xml

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.

Deploy webapp manually with Tomcat (ie autoDeploy=false, noDeployOnStartup=false)

I'm trying to deploy several web applications to tomcat 6.x, and I've turned off autoDeploy and onDeployStartup because I want to manually register these apps and map them to URLs not based on the names of their war files.
I've put the following context file in $catalina.home/conf/Catalina/localhost:
<Context path="" docBase="web-1.0-SNAPSHOT.war" debug="1">
</Context>
And I put the war file under $catalina.home/webapps, but when I startup tomcat nothing gets deployed. I don't even see any error messages about the context files I created. Or any print outs saying anything is wrong.
What's the problem? I've read the documents which outlines autodeploy a lot, but is very sketchy on details of how to do this outside of autodeploy.
So the details about how autoDeploy works, and alternative deployments is only really discussed here.:
http://wiki.apache.org/tomcat/HowTo#How_do_I_make_my_web_application_be_the_Tomcat_default_application.3F
I don't know why tomcat makes this so complicated. If you turn off autoDeploy your only option is to modify the server.xml and add your contexts there. You can't externalize the definitions of your contexts which seems convoluted way to deploy things. If I'm going to take the time to drop a XML config file I should be able to control the URL it's mounted to and the docBase. Just make it straight forward because Jetty does.
Try the following steps
Shutdown the tomcat
Copy web-1.0-SNAPSHOT.war to webapps folder.
Deploy the webapp.
Now there is a folder named web-1.0-SNAPSHOT inside webapps.
go to conf/server.xml
Add the following entries
<Context path="/abc" docBase="web-1.0-SNAPSHOT" debug="1"></Context>
The docbase doesn't have the .war extention. When web-1.0-SNAPSHOT.war is deployed there will be a directory web-1.0-SNAPSHOT inside webapps. The docbase should point to this directory.
Please make sure that Context tag is within the
<Host> </Host> tag
<Host>
<Context path="/abc" docBase="web-1.0-SNAPSHOT" debug="1"></Context>
</Host>
After editing server.xml you have to restart tomcat server to reflect the changes.
Now you can find your webapp at
localhost:8080/abc
Hope this helps
Setting deployonstartup to false tells tomcat not to deploy apps on startup. I think its enough to turn autodeploy off. so maybe try the following in the Host in server.xml: autoDeploy="false" deployOnStartup="true".
Example code
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="false" deployOnStartup="false">
<Context path="/howto-prepareexam" docBase="howto-prepareexam" debug="1"></Context>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
Then restart tomcat

Categories

Resources