11

I read about this on Tomcat guide here and some SO questions. And I think I'm pretty much doing the same thing. But in some way cannot manage to succeed.

First of all I have to say that my application is deployed on a shared Tomcat server that I have no control over. I just drop my .war file and it gets deployed.

I tried to package my application as ROOT.war but didn't work. The admin told me to package it as whatever the name I want and they would take care of it. I packaged it as my-application.war and it got deployed but I have to enter http://my-host/my-application to get to the website.

After contacting the admin they told me that they have put a context elemnt in my host in Tomcat config file like:

<Context path="" docBase="path of my-application deployed folder"/>

which was supposed to set my-application as default application for all the requests coming to my-host. But it didn't and whenever I enter http://my-host I get:

HTTP Status 404 - / The requested resource (/) is not available

But again when I enter http://my-host/my-application it all works fine. Any suggestion on what might be wrong is definitely appreciated.

Updates:
I tried to follow the steps described in tomcat documentation on how to make the application default. 3 ways are described and I tried all three ways and could successfully deploy my application as ROOT on localhost.

I also tried to reproduce the problem I'm facing on remote server so I could find the reason and report it to admin. I find couple of problems.

  1. In server.xml fragment that admin sent me autoDeploy and deployOnStartUp are set to true, whereas they should be false if explicitly defining Context element in server.xml. This will cause double deployment which creates a ROOT folder and a folder with the name of the .war file. Deleting the .war will delete it's corresponding folder and undeploys the application but ROOT remains and must be deleted manually and requires a Tomcat restart. Until it's restarted any deployment of ROOT.war will fail.
  2. I figured there are some reasons preventing ROOT.war from deploying. One could be that a ROOT.xml exists in conf/{engine-name}/{host-name} or a ROOT folder exists in appBase of the host or as I described above a ROOT application from previous deployment is not undeployed and requires Tomcat restart.

Either way I couldn't exactly pinpoint what exactly is preventing ROOT.war from deploying since that requires the access to Tomcat log files and conf files to check the cases I described above.

Also from all I see my the admin seems incapable of maintaining a Tomcat server and finding the problem. So I decided to go with a dedicated Tomcat server after struggling with the shared one.

doctrey
  • 1,227
  • 1
  • 12
  • 25
  • Is Tomcat being used standalone and serving HTTP or is there another server handling that and communicating to Tomcat via AJP? It sounds like there is some virtual hosting going on. Is that defined in Tomcat's main `server.xml` or done as separate host specific configuration file under `conf/Catalina`? – laz Dec 12 '11 at 05:59
  • I just tried adding the context path to my tomcat's server.xml file and it did the redirect like a champ. All I did was put docBase="my-application" and not an absolute path or anything like that. In your sample code above, is that what "path of my-application deployed folder" means? – Jeff Goldberg Dec 13 '11 at 13:45
  • Also, do you have access to the whole element which contains the element? Depending on its settings, it may prevent the context settings from working like you'd expect. – Jeff Goldberg Dec 13 '11 at 14:31
  • @JeffGoldberg The path above is absolute. I don't have access to element but I have seen it and it has it's default attributes. Inside there's only the element I described above. I'll try to ask the Admin to give it a relative path instead. – doctrey Dec 14 '11 at 08:39

3 Answers3

6

In your question, you state that the admin set the context as:

<Context path="" docBase="path of my-application deployed folder"/>

Based on the comments above, I would suggest trying to use the relative path of your application rather than the absolute path.

I tried this on my tomcat server with:

<Context path="/" docBase="my-application/" />

and that did the trick.

The Host element which contains the Context element does actually set some parameters that might also impact the context. If it's the default settings, then a relative context should simply point to the webapps folder. If it's been changed, the results may vary.

Jeff Goldberg
  • 931
  • 1
  • 9
  • 20
4

Usually this can be achieved by the following steps:

  • Define a ROOT.xml context file in conf/Catalina/localhost
  • Name your webapp WAR “ROOT.war” or containing folder “ROOT”

However, i doubt you will be able to do that on a shared Tomcat instance. Only one application can run as the default application. The hosting company will probably not allow it as otherwise which application will they allow to be the default out of the many people sharing the same Tomcat instance?

See this link : http://staraphd.blogspot.com/2009/10/change-default-root-folder-in-tomcat.html

ziggy
  • 15,677
  • 67
  • 194
  • 287
  • Thanks for the reference. But I don't have control over most of the parts the article is suggesting. And I think the admin has already set the context part up as I have mentioned. – doctrey Dec 04 '11 at 12:54
  • If the admin has set your context path...there is your answer, you can't do this without having the user come in on some "folder" like http//myapp.com/myapp. The only way you can get past this is to get the admin to deploy you as ROOT.war or get your war pointed to from the ROOT.xml. – Bob Kuhar Dec 11 '11 at 20:33
  • @Bob According to [here](http://wiki.apache.org/tomcat/HowTo#How_do_I_make_my_web_application_be_the_Tomcat_default_application.3F) the method I described above is supposed to set the application as default. Though I'm not clear on this whether it will override the ROOT application and becomes the new ROOT or it just acts as default for applications that are not assigned to ROOT and any other context already defined which the latter doesn't make sense I think. – doctrey Dec 12 '11 at 16:40
3

The Tomcat Wiki has a section on putting applications into default context. However, in order to do this it implies some control over the Tomcat server that may not be possible in the shared context you describe.

If you have the ability to install other systems on the server, one alternate solution would be to use a proxy server like NGINX. This is way more complicated than simply naming your war file ROOT.war, but sometimes it's the only option.

If you have NGINX listening on the server and you have your own url, you use the HttpProxyModule with setting such as:

server {
        listen          80;
        server_name     my.domain.com;
        location / {
            proxy_pass              http://my-host/my-application;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        Host $http_host;
        }
    }

Also in order to make this work, you'd have to own the "my.domain.com" url and it would need to be separate from the one everyone is using for the shared Tomcat server.

The nginx portion of the solution is free, but if you need to register a new url and then use something like no-ip.com to redirect it to the tomcat server it would cost money.

Jeff Goldberg
  • 931
  • 1
  • 9
  • 20
  • I think I rather stick with configuring Tomcat to get the results. Thanks anyway. – doctrey Dec 04 '11 at 13:26
  • Yeah, I figured. This solution is really more for a scenario where you have a lot of sites and a lot of URLs to redirect. But I thought it might be helpful to someone. – Jeff Goldberg Dec 04 '11 at 15:08
  • It worked for me. Thanks for the tip. I was really annoyed by the "context" of every application at the end of the URL. – alfredocambera Jan 31 '13 at 19:07