REST WEB SERVICE URI [duplicate] - java

I have the war file of my application. I need to deploy this at the root level. The current URL is http://localhost:8080/war_name/application_name.

You have a couple of options:
Remove the out-of-the-box ROOT/ directory from tomcat and rename your war file to ROOT.war before deploying it.
Deploy your war as (from your example) war_name.war and configure the context root in conf/server.xml to use your war file :
<Context path="" docBase="war_name" debug="0" reloadable="true"></Context>
The first one is easier, but a little more kludgy. The second one is probably the more elegant way to do it.

on tomcat v.7 (vanilla installation)
in your conf/server.xml add the following bit towards the end of the file, just before the </Host> closing tag:
<Context path="" docBase="app_name">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
Note that docBase attribute. It's the important bit. You either make sure you've deployed app_name before you change your root web app, or just copy your unpacked webapp (app_name) into your tomcat's webapps folder. Startup, visit root, see your app_name there!

In tomcat 7 with these changes, i'm able to access myAPP at / and ROOT at /ROOT
<Context path="" docBase="myAPP">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
<Context path="ROOT" docBase="ROOT">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
Add above to the <Host> section in server.xml

I know that my answer is kind of overlapping with some of the other answer, but this is a complete solution that has some advantages. This works on Tomcat 8:
The main application is served from the root
The deployment of war files through the web interface is maintained.
The main application will run on port 80 while only the admins have access to the managment folders (I realize that *nix systems require superuser for binding to 80, but on windows this is not an issue).
This means that you only have to restart the tomcat once, and after updated war files can be deployed without a problem.
Step 1:
In the server.xml file, find the connector entry and replace it with:
<Connector
port="8080"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector
port="80"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
Step 2:
Define contexts within the <Host ...> tag:
<Context path="/" docBase="CAS">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
<Context path="/ROOT" docBase="ROOT">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
<Context path="/manager" docBase="manager" privileged="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
<Context path="/host-manager" docBase="host-manager" privileged="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
Note that I addressed all apps in the webapp folder. The first effectively switch the root and the main app from position. ROOT is now on http://example.com/ROOT and the the main application is on http://example.com/. The webapps that are password protected require the privileged="true" attribute.
When you deploy a CAS.war file that matches with the root (<Context path="/" docBase="CAS"> you have to reload that one in the admin panel as it does not refresh with the deployment.
Do not include the <Context path="/CAS" docBase="CAS"> in your contexts as it disables the manager option to deploy war files. This means that you can access the app in two ways: http://example.com/ and http://example.com/APP/
Step 3:
In order to prevent unwanted access to the root and manager folder, add a valve to those context tags like this:
<Context path="/manager" docBase="manager" privileged="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
addConnectorPort="true"
allow="143\.21\.2\.\d+;8080|127\.0\.0\.1;8080|::1;8080|0:0:0:0:0:0:0:1;8080"/>
</Context>
This essentially limits access to the admin web app folder to people from my own domain (fake IP address) and localhost when they use the default port 8080 and maintains the ability to dynamically deploy the war files through the web interface.
If you want to use this for multiple apps that are using different IP addresses, you can add the IP address to the connector (address="143.21.2.1").
If you want to run multiple web apps from the root, you can duplicate the Service tag (use a different name for the second) and change the docbase of the <Context path="/" docBase="CAS"> to for example <Context path="/" docBase="ICR">.

Remove $CATALINA_HOME/webapps/ROOT. Update $CATALINA_HOME/conf/server.xml, make sure that Host element look like the following text:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="false" deployOnStartup="false">
<Context path="" docBase="myApp"></Context>
It works with Tomcat 8. autoDeploy and deployOnStartup need to set to false to prevent tomcat from deploying myApp twice.

The fastest way.
Make sure you don't have ROOT app deployed, undeploy if you have one
Rename your war to ROOT.war, deploy, thats all, no configuration changes needed

Adding to #Dima's answer, if you're using maven to build your package, you can tell it to set your WAR file name to ROOT in pom.xml:
<build>
<finalName>ROOT</finalName>
</build>
By default, tomcat will deploy ROOT.war webapp into root context (/).

Adding on to #Rob Hruska's sol, this setting in server.xml inside section works:
<Context path="" docBase="gateway" reloadable="true" override="true"> </Context>
Note: override="true" might be required in some cases.

open tomact manager url :- http://localhost:8080/manager/html/
then in applications you see a application having path as "/" is deployed simply Undeploy this.
Rename your application's war file as ROOT.war and just place at path :- C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps
start your Tomcat No extra configuration needed.
Now we can see our application home page or configured url at http://localhost:8080

In my server I am using this and root autodeploy works just fine:
<Host name="mysite" autoDeploy="true" appBase="webapps" unpackWARs="true" deployOnStartup="true">
<Alias>www.mysite.com</Alias>
<Valve className="org.apache.catalina.valves.RemoteIpValve" protocolHeader="X-Forwarded-Proto"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="mysite_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b"/>
<Context path="/mysite" docBase="mysite" reloadable="true"/>
</Host>

Related

Tomcat mapping context via server.xml is not working in Tomcat 8.5

I have define web directory path in docBase so that tomcat directly access files and run it.
My web directory path is 'D:\Test\WebContent' where Test is my project name.
I am declaring context tag under Host tag in server.xml file of tomcat.
After start tomcat whenever I hit URL 'http://localhost:8080/Test' get error HTTP Status 404 – Not Found.
I am using Tomcat 8.5
<Host name="localhost" appBase="webapps" unpackWARs="false" autoDeploy="true">
<Context path="/Test" docBase="D:\Test\WebContent" reloadable="true" />
</Host>

How set context variable in a web app's context.xml when it's also the default web app?

I have a WAR that I've set as the default web app in Tomcat's server.xml:
Inside Server.xml
<Host...>
<Context docBase="mywar" path="" />
In addition, I have a META-INF/context.xml file where I set an environment variable.
<?xml version="1.0" encoding="utf-8"?>
<Context debug="0" reloadable="true">
<Environment
name="my.name"
type="java.lang.String"
value="donrhummy"
/>
</Context>
But it's throwing an exception javax.naming.NameNotFoundException: Name my.name is not bound in this Context.
How can I get that context variable in my app?
NOTE: That variable exists when I go to http://localhost/mywar
The only solution i could find was:
Rename my WAR as ROOT.war (Case-sensitive)
Remove the <Context...> from server.xml
Tomcat will treat that as the root WAR (i.e. for the "/" path) and let it use the context form it's own META-INF directory.

Unable to get static image, Tomcat 8.0.24

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.

Setting property 'source' to 'org.eclipse.jst.jee.server:GestorContenidoWS' did not find a matching property. Try all the solutions

This is my first post here (but not the first time that I visit the page, I found a lot of solutions here), first of all, sorry for my english I will try to explain myself as best I can.
This question appears another time in this page, but I tried all the solutions that the people post and I still with this problem, well here we go.
I made a project on Eclipse(indigo) for launch like a WebService (I did it before with succes, is not my first time) and when I Run on Server(Tomcat7) all seems fine and the appears this Warning.
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:GestorContenidoWS' did not find a matching property.
Then appear in the Web perspective of Eclipse the page "HTTP 404". I'm telling this because I read in other post that this Warning is not a problem, but seems that it is for me (The project is also vinculated with a JPA Persistence).
.I found two solutions for make dissappear this Warning. First, go to Server Overview and select the option 'Publish module contexts to separate XML files' and then try to Run on server again, but didn't work.
The other option was, remove the project from the server from the Server View. Then run the project under the same server for recreated server.xml, but didn't work also.
Anybody can help me? Maybe the problem of this "HTTP 404 requested resource (/GestorContenidoWS/) is not available." is in another part or its because this Warning?"
The code of my server.xml (without comments) is this:
<?xml version="1.0" encoding="UTF-8"?>
--><Server port="8005" shutdown="SHUTDOWN">
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.core.JasperListener"/>
<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 docBase="GestorContenidoWS" path="/GestorContenidoWS" reloadable="true" source="org.eclipse.jst.jee.server:GestorContenidoWS"/></Host>
</Engine>
</Service>
Thank you
First of all this is a warning and not an error. So there is no need to worry too much about it. Basically it means, that Tomcat does not know what to do with the source attribute from context.
This source attribute is set by Eclipse (or to be more specific the Eclipse Web Tools Platform) to the server.xml file of Tomcat to match the running application to a project in workspace.
Tomcat generates a warning for every unknown markup in the server.xml (i.e. the source attribute) and this is the source of the warning. You can safely ignore it.
This is a warning and can be safely ignored. Tomcat or the Web container does not understand what to do with the property set by Eclipse
set contex-root
<property name="context-root" value="path" />
and http://localhost:8080/path
show your index file
Find server.xml. For example "C:\Dev\apache-tomcat-8.5.23\conf\server.xml"
Find your application in server.xml
<Context docBase="C:\Dev\apache-tomcat-8.5.23\wtpwebapps\SpringRestExample" path="/spring" reloadable="true" source="org.eclipse.jst.jee.server:SpringRestExample"/></Host>
Expected url http://localhost:8080/SpringRestExample/rest/emp/9999
But (path="/spring") instead (path="/SpringRestExample")
Therefore current url http://localhost:8080/spring/rest/emp/9999

How do I import the Host directive for Tomcat Server.xml from another file?

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

Categories