I created a dynamic web project and added a context.xml file to the META-INF folder as mentioned here. But when I deploy the war file the context file is not copying to the folder $CATALINA_BASE/conf/[enginename]/[hostname]/. My context.xml file content are following,
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Environment name="test" value="10"
type="java.lang.Integer" override="false"/>
</Context>
Why this approach is not working? How can I set the context of a application dynamically?
Try updating the Host element inside your tag.
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
...
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true" copyXML="true">
Related
I have Tomcat 9. I am trying to get the URL lik this: http://default.local:8080/default_war/ but my site is still available only by http://default.local:8080.
The address I am going to set is also generated by Intellij in Run/Configuration so it shoud be possible to set it. Of course I could edit it in Intellij but I wonder how to achieve URL subderictories in Tomcat 9.
What should I change in my code? Here there are my setting files:
$ cat /home/u/opt/tomcat/conf/server.xml
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<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 name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="default.local" appBase="default"
unpackWARs="true" autoDeploy="true">
<Context path="/default_war" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
u#i3 ~/opt/tomcat
$ cat conf/Catalina/default.local/ROOT.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="${catalina.base}/deploy/default_war.war">
</Context>
u#i3 ~/opt/tomcat
$
This is happening because you are using a context file called ROOT.xml.
The ROOT application is the one which is served from the base URL - in your case, http://default.local:8080.
Depending on exactly how you deployed your WAR, you will need to take the following steps:
1 - Stop Tomcat and change the name of the ROOT.xml file to default_war.xml.
The above step means you are no longer using a ROOT-based webapp (see final note below for more on that).
2 - Because you have specified appBase="default" in your server.xml, you may also need to go to that default folder and remove any sub-folders in there, before restating Tomcat.
I would expect there to be a ROOT folder in there, because you have already used a ROOT.xml context file. You don't want that folder, because that will continue to serve your webapp from the base URL.
When you restart Tomcat, you should see a new folder called default_war in the default folder - and that is where your application will be served from - and it should be available only at http://default.local:8080/default_war/.
Background note: In Tomcat, ROOT is something of a special case. When you download and install a fresh installation of Tomcat, you will see a folder called ROOT in the webapps directory. This is where the main Tomcat welcome/congratulations page is served from - and like your ROOT.xml content, it is served from the base URL. You can see some of the possible configurations in the "naming" table shown in this Tomcat documentation page.
I created a RESTful web service using auto generated entity classes with netbeans. I also used maven. I deployed this in tomcat 8 as localhost. I do see the index page, but I cannot access any endpoints. I initially ran this locally on Glassfish server and it worked fine.
I tried test resource uri in netbeans but it says,
Unable to open resource URL : localhost:8080//api/activity
Here is my server.xml file from apache tomcat.
<?xml version="1.0" encoding="UTF-8"?>
--><Server port="8005" 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="8080" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8009" 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 antiJARLocking="true" docBase="C:\xxx\yyyy\apache-tomcat-8.0.36\apache-tomcat-8.0.36\wtpwebapps\xxDBWithMaven" path="/VoyateDBWithMaven" reloadable="true" source="org.eclipse.jst.jee.server:VoyateDBWithMaven"/></Host>
</Engine>
</Service>
</Server>
Is there anyway I can fix this so the resources can be accessed? It's confusing because the application gets deployed and the index file is displayed.
Unable to open resource URL : localhost:8080//api/activity
You have two slashes between "localhost:8080" and "api/activity". Remove one in the configuration where you specify the path: api/activity instead of /api/activity.
I have an application with the back-end that is implemented using Spring MVC and front end - AngularJs.
I would like to display uploaded images at my front end. The most simple way, as far as I googled it out, is to edit the server.xmlat the folder apache-tomcat-8.0.24\conf by adding the following line:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="C:\images_test" path="/my_project/images"> <-- this line was added by me.
</Context>
</Host>
After it, I could simply access the image by the following URI: http://localhost:8080/my_project/images/pic.jpg
However, I cannot do it. The browser displays me 404 error status. How can I access this picture from my browser and possibly from <img>?
Should I redeploy the application to the Tomcat server or should I restart the server after modifying the server.xml filE?
If Tomcat is for example installed on C: then the /path/to/files would actually point to C:\path\to\files. Thus below should be in your code.
<Context docBase="/images_test" path="/my_project/images">
Above docbase would point to C:\images_test
I think something like this should work for you :
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="C:\path_to_project" path="/my_project" aliases="/images=C:\images_test">
</Context>
</Host>
Then, just try something like http://localhost:8080/my_project/images/pic1.jpg.
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="C:\images_test" path="" />
</Host>
and you should be able to access your image via http://localhost:port/pic1.jpg
the issue was in my IDE - Intellij IDEA, it creates a custom server.xml for each web application.
I have the following configuration running on Tomcat 8.0.20:
C:\tomcat
conf
server.xml
context.xml
webapps
app
app2
......
C:\external
app3
web
app4
web
app5
web
In context.xml I have <Context crossContext="true"> .... </Context>
And in server.xml:
..........
<Host name="host.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context
cachingAllowed="false"
docBase="C:\external\app3\web"
path=""
crossContext="true"
reloadable="true" />
<Context
cachingAllowed="false"
crossContext="true"
docBase="C:\external\app4\web"
path="/app4"
reloadable="true" />
<Context
cachingAllowed="false"
crossContext="true"
docBase="C:\external\app5\web"
path="/app5"
reloadable="true" />
</Host>
Obtaining the Context of /app, /app2 or /app5 from /app4 works as espected:
request.getServletContext().getContext("/app") is not null.
However, requesting the Context of /app3 (with context path /).
Any idea why getContext("/") is null?
As it turned out, it's a regression in tomcat after version 8.0.15
Filed a bug report here
Reverted tomcat to 8.0.15 and it's working now....
My company wants to be able to add other Hosts directives into our server.xml (configuration file for Tomcat). This Host directive goes inside the Engine directive. I will like to import a second file, example hosts.xml, so I can define the hosts in that separate files.
<Host name="localhost" ...>
...
<Valve className="org.apache.catalina.valves.AccessLogValve"
prefix="localhost_access_log." suffix=".txt"
pattern="common"/>
...
</Host>
I have looked into the Professional Apache Tomcat book by WROX ISBN: 0-7645-4372-5 and there was no answer there.
You do that by placing context xml files in the appropriate place:
${catalina.home}/conf/Catalina/www.example.com/ROOT.xml
${catalina.home}/conf/Catalina/www.foobar.com/ROOT.xml
${catalina.home}/conf/Catalina/www.foobar.com/other-webapp.xml
Unfortunately your server.xml will still have to contain the root host elements:
<Engine defaultHost="www.example.com" name="Catalina">
<Host name="www.example.com"></Host>
<Host name="www.foobar.com"></Host>
...