When I have specified multiple host tags in the conf/server.xml, the servlet context is loading twice. It is spring based web application. I have extended the spring ContextLoader and customize it.
My server.xml (tomcat 7.0.22)
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/mnt/databank/logs/tomcat7"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/>
</Host>
<Host name="domain.com" appBase="webapps/Domain"
unpackWARs="true" autoDeploy="true"
>
<Context path="" docBase="."/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/mnt/databank/logs/tomcat7"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/>
<Context docBase="/mnt/databank/posters" path="/Domain/posters"/>
<Context docBase="/mnt/databank/advertisement" path="/Domain/advertisement"/>
<Context docBase="/mnt/databank/star" path="/Domain/star"/>
</Host>
<Host name="www.domain.com" appBase="webapps/Redirecter"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="."/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/mnt/databank/logs/tomcat7"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/>
</Host>
I have added a log in the ContextLoader it trigering twice as follows,
2011-10-21 12:11:22,933 ERROR [Thread-2] c.i.b.u.DomainUtilInitializer [DomainUtilInitializer.java:38] Init Method Triggered
2011-10-21 12:11:46,621 ERROR [Thread-15] c.i.b.u.DomainUtilInitializer [DomainUtilInitializer.java:38] Init Method Triggered
Any help would be appreciated.
This seems like it should be expected behavior - you have configured Tomcat to run the application twice, once for each host. What do you expect? If you want a single instance of the webapp running, then you shouldn't configure two <host>s like so - I believe you add a host name alias to the single/default <Host>.
Related
I am using apache apache-tomcat-8.5.6 for deploying my web applications. I have a webapps folder and a ROOT.war file under webapps and all my domains by default access this ROOT.war file.
Now I am planning to add two more folders similar to webapps, let's say folder1 and folder2 and planning to deploy ROOT.war under both these folders.
Now I want a certain domain to access ROOT.war under folder1 and another to access folder2. All other domains by default should access ROOT.war under webapps.
I believe that this is possible and might need some changes under the server.xml file. I am new to server configurations. Is there any good documentations or su
This is absolutely possible.
The key of this solution is that the Engine-Tag attribute "defaultHost" names the application-Host which is used by default and the Host-Tag attribute "name" contains the domain name which uses this application if it matches.
Example server.xml:
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource
auth="Container"
description="Database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
name="UserDatabase" pathname="conf/tomcat-users.xml"
type="org.apache.catalina.UserDatabase" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true" />
<Engine defaultHost="defaultHost" name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
<Host
name="defaultHost"
appBase="webapps-leave-empty">
<Valve
className="org.apache.catalina.valves.AccessLogValve"
directory="${catalina.base}/logs/access/"
rotatable="true"
buffered="true"
prefix=""
suffix="-access.log"
fileDateFormat="yyyyMMdd"
pattern='%a %t %v "%r" %s %b "%{Referer}i" "%{User-Agent}i" %I' />
<Context docBase="<PathToDefaultApplication>" path="" />
</Host>
<Host
name="<SpecialDomain1>"
appBase="webapps-leave-empty">
<Valve
className="org.apache.catalina.valves.AccessLogValve"
directory="${catalina.base}/logs/access/"
rotatable="true"
buffered="true"
prefix=""
suffix="-access.log"
fileDateFormat="yyyyMMdd"
pattern='%a %t %v "%r" %s %b "%{Referer}i" "%{User-Agent}i" %I' />
<Context docBase="<PathToApplication1>" path="" />
</Host>
<Host
name="<SpecialDomain2>"
appBase="webapps-leave-empty">
<Valve
className="org.apache.catalina.valves.AccessLogValve"
directory="${catalina.base}/logs/access/"
rotatable="true"
buffered="true"
prefix=""
suffix="-access.log"
fileDateFormat="yyyyMMdd"
pattern='%a %t %v "%r" %s %b "%{Referer}i" "%{User-Agent}i" %I' />
<Context docBase="<PathToApplication2>" path="" />
</Host>
</Engine>
</Service>
</Server>
java.lang.IllegalArgumentException: Document base /var/lib/tomcat7/webapps/Monocle does not exist or is not a readable directory
at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:137)
at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:5197)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5386)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Out tomcat7 is failing to start. to find if the issue is with tomcat itself or with my code, I deleted all the war files from the webapps folder except ROOT folder. When I started, it failed throwing the above error. Monocle is one war that I deleted. I don't understand why it is still trying to find Monocle. I already deleted Catalina folder from work folder. Can someone help me? do I need clear caches from any other location?
You are able to verify TOMCAT_HOME/conf/server.xml file
In the tag, appBase attribute has default value is wepapps, mabe you changed appBase="/var/lib/tomcat7/webapps/Monocle".
Hope this helps!!!
ps: if not, add more information such. e.g:TOMCAT_HOME/conf/server.xml, TOMCAT_HOME/conf/context.xml
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
To be precise I need to get pictures from an external directory say: C:/MWDB.
I found this link: How to config Tomcat to serve images from an external folder outside webapps? which I wanna follow up with the part where you need to add a directory in your server.xml context tag.
So Basically Based on the way it says I came with that it should be like:
<Context docBase="C:/MWDB" path="/images" reloadable="true" source="org.eclipse.jst.jee.server:Duck"/> (request: www.example.com/images/image.png) with this way while restaring the server it pops me an error saying
The server at localhost has encountered a problem
Which then I did this: <Context docBase="C:/MWDB" path="/images"/> And This I can't even access my index.html file
FULL server.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Server port="20011" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<GlobalNamingResources>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="20012" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="20013" protocol="AJP/1.3" redirectPort="8443"/>
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>
<Context docBase="C:/MWDB" path="/images" reloadable="true" source="org.eclipse.jst.jee.server:Duck"/></Host>
</Engine>
</Service>
</Server>
I'm using Java 8, Apache Tomcat v9.0
Okay I figured out my problem.
Basically What I didn't know is that you could have multiple <context> at the same time.
So When I had something like:
<Context docBase="C:/MWDB" path="/images"/>
I could Server from only MWDB directory and not my internal Project and that is why I couldn't access my index.html file. So For to server from both directory Simply you can do:
<Context docBase="C:/MWDB" path="/images" reloadable="true" source="org.eclipse.jst.jee.server:Duck"/>
<Context docBase="C:/MWDB" path="/images"/>
Works Perfectly so far :)
I have a java web app that I deploy in production with Tomcat7.
I use the Tomcat Web Application Manager page in production, where I deploy my WAR at the context path "/". In production I'm not seeing jsessionid in URL.
In my development environment though, the same application (hence the same web.xml), started with Tomcat7 inside eclipse is showing jsessionid in URL.
The only session configuration I have in my web.xml is:
<session-config>
<session-timeout>15</session-timeout>
</session-config>
The only difference I can see in both Tomcat7 is the server.xml:
Production:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
</Host>
Local:
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
<Context docBase="MyApp" path="/" reloadable="true" source="org.eclipse.jst.jee.server:MyApp" />
</Host>
Another difference is that I use NGinx in production to do a proxy pass from port 80 to 8080 from Tomcat.
What may I be missing?
Thanks
You may using a browser or other client that doesn't support (or disabled) cookies in your development environment.
Another Tip: you may use this code in tomcat 7 (in your web.xml file):
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
The problem is that for cookies to work properly, the domain name must have at least two dots (https://curl.haxx.se/rfc/cookie_spec.html).
So to make it work locally, I had to do the following:
Change the /etc/hosts file to include any domain name with at least one period, pointing to 127.0.0.1:
127.0.0.1 localhost
127.0.0.1 localhost.test
255.255.255.255 broadcasthost
::1 localhost
Change the context.xml to include the new domain:
<Context sessionCookieDomain=".localhost.test" sessionCookiePath="/">
...
</Context>
I have installed Apache tomcat 8 on my windows server 2012 where I need to store my Java applications. I have 3 apps: app1, app2, app3
Right now if I put into my - Tomcat 8.0\webapps folder war file called ROOT.war app starts on address mydomain.com and works fine - but I need to be able to run several apps at the same tomcat - Thats's why I want to create subdomains
app1 should launch on app1.mydomain.com
app2 should launch on app2.mydomain.com
app3 should launch on app3.mydomain.com
How can I fulfill this?
I search similar topics here on stack-overflow but nothing helped.
Please help.
Actually I solved my issue like this:
I have configured server.xml file located in my case in Program Files (x86)\Apache Software Foundation\Tomcat 8.0\conf folder
found there this code:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
made a copy of it and replaced the values with my subdomains:
<Host name="app1.mydomain.com " appBase="app1.mydomain.com "
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
and that's it actually.
For myself I also removed default localhost HOST and now I have only subdomains.