How to change the ROOT application?
Asked Answered
R

13

138

I'm trying to change the default application of a Tomcat 6 webserver to a different application than "ROOT" (inside webapps folder). What is the best way to do this?

Rosalba answered 3/4, 2009 at 20:5 Comment(0)
H
134

There are three methods:

  • First shutdown your Tomcat from the its bin directory (sh shutdown.sh). Then delete all the content of your Tomcat webapps folder (rm -fr *). Then rename your WAR file to ROOT.war, and finally start your Tomcat from the bin directory (sh startup.sh).

  • Leave your war file in $CATALINA_BASE/webapps under its original name. Turn off autoDeploy and deployOnStartup in your Host element in the server.xml file. Explicitly define all application Contexts in server.xml, specifying both the path and docBase attributes. You must do this because you have disabled all the Tomcat auto-deploy mechanisms, and Tomcat will not deploy your applications anymore unless it finds their Context in the server.xml.

    second method: in order to make any change to any application, you will have to stop and restart Tomcat.

  • Place your WAR file outside of $CATALINA_BASE/webapps (it must be outside to prevent double deployment). Place a context file named ROOT.xml in $CATALINA_BASE/conf/. The single element in this context file MUST have a docBase attribute pointing to the location of your WAR file. The path element should not be set - it is derived from the name of the .xml file, in this case ROOT.xml. See the documentation for the Context container for details.

Reference

Hardhack answered 23/5, 2011 at 6:37 Comment(4)
I tried the first method and it works! Bdw - you DON'T need to delete everything. I just deleted the ROOT folder and renamed my war to ROOT.war and it works like a charm.Nibble
I know its not really following the sof rules but screw it..... Thanks! I wish I could upvote more than once. Super helpful. BTW, i had to restart tomcat before this would work, @hostnik.Ladysmith
@danny-london's third method didn't work for me in Tomcat 7, but it does work if you place the ROOT.xml file in the $CATALINA_BASE/conf/[enginename]/[hostname] directory (which for my rather basic setup is $CATALINA_HOME/conf/Catalina/localhost). Refer to the Context Container documentation.Thetos
Modern alternative to starting Tomcat: sudo systemctl start tomcatx where x stands for the version numberLatchstring
J
26

Adding a <Context> tag in the <Host> tag in server.xml for Tomcat 6 will resolve the problem.

If you use path="" empty you can use a URL like http://localhost/first.do.

In the context tag set attributes docBase="E:\struts-ITRCbook\myStrutsbook" and reloadable="true", then end the context tag.

It should look something like this:

<Host name="localhost"  appBase="webapps" 
        unpackWARs="true" autoDeploy="true"
        xmlValidation="false" xmlNamespaceAware="false">
    <Context path="" docBase="E:\struts-ITRCbook\myStrutsbook" reloadable="true">
    </Context>
</Host>
Jackqueline answered 3/3, 2010 at 8:11 Comment(3)
is this also true for tomcat 7?Libelous
Per the documentation: For Tomcat 6, unlike Tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. This is because it makes modifying the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat.Gschu
let notify this approach has side effect! myStrutsbook will deploy twice Once for localhost:8080 and once for localhost:8080/myStrutsbook ! and may cause database connection errors and more resource usageEntente
B
26

In Tomcat 7 with these changes, i'm able to access myAPP at / and ROOT at /ROOT

<Context path="" docBase="myAPP"/>
<Context path="ROOT" docBase="ROOT"/>

Add above to the <Host> section in server.xml

Brock answered 14/8, 2013 at 8:22 Comment(2)
Just the solution I was looking for! Thanks a million, Sudheer! Works like a charm for me. And hey! I have another question. When I do access the ROOT by /ROOT and try logging into the Manager page using the correct credentials, I'm denied access. Would you know how to fix this?Voltz
@Sudheer Palyam I did the same. I changed the server.xml file, and then I started the server. But when I loaded/visited the localhost:8080, it showed me 404 status error. If I delete these lines from server.xml, then It load the above port.Autolysis
G
17

ROOT default app is usually Tomcat Manager - which can be useful so I felt like keeping it around.

So the way i made my app ROOT and kept TCmgr was like this.

renamed ROOT to something else

mv ROOT TCmgr

then created a symbolic link whereby ROOT points to the app i want to make the default.

ln -s <your app> ROOT

worked for me and seemed the easiest approach.

Grandstand answered 3/11, 2011 at 17:7 Comment(1)
@Vic you are right, but windows does have symbolic links, in XP and above I think you can use mklink. Please note I have not done this and there my be other reasons not to use symbolic links.Intimate
V
14

You can do this in a slightly hack-y way by:

  1. Stop Tomcat
  2. Move ROOT.war aside and rm -rf webapps/ROOT
  3. Copy the webapp you want to webapps/ROOT.war
  4. Start Tomcat
Viipuri answered 3/4, 2009 at 21:9 Comment(1)
Ugly or maybe icky, but hacky might be the wrong term since that appears to be the official/supported method.Debtor
S
13

According to the Apache Tomcat docs, you can change the application by creating a ROOT.xml file. See this for more info:

http://tomcat.apache.org/tomcat-6.0-doc/config/context.html

"The default web application may be defined by using a file called ROOT.xml."

Sicyon answered 3/4, 2009 at 20:9 Comment(0)
F
3

An alternative solution would be to create a servlet that sends a redirect to the desired default webapp and map that servlet to all urls in the ROOT webapp.

package com.example.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RedirectServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.sendRedirect("/myRootWebapp");
  }
}

Add the above class to
CATALINA_BASE/webapps/ROOT/WEB-INF/classes/com/example/servlet.
And add the following to
CATALINA_BASE/webapps/ROOT/WEB-INF/web.xml:

  <servlet>
    <display-name>Redirect</display-name>
    <servlet-name>Redirect</servlet-name>
    <servlet-class>com.example.servlet.RedirectServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Redirect</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

And if desired you could easily modify the RedirectServlet to accept an init param to allow you to set the default webapp without having to modify the source.

I'm not sure if doing this would have any negative implications, but I did test this and it does seem to work.

Falstaffian answered 11/7, 2011 at 18:2 Comment(0)
H
2

the context.xml configuration didn't work for me. Tomcat 6.0.29 complains about the docBase being inside the appBase: ... For Tomcat 5 this did actually work.

So one solution is to put the application in the ROOT folder.

Another very simple solution is to put an index.jsp to ROOT that redirects to my application like this: response.sendRedirect("/MyApplicationXy");

Best Regards, Jan

Holmann answered 11/1, 2011 at 16:38 Comment(0)
D
1

I've got a problem when configured Tomcat' server.xml and added Context element. He just doesn't want to use my config: http://www.oreillynet.com/onjava/blog/2006/12/configuration_antipatterns_tom.html

If you're in a Unix-like system:

  1. mv $CATALINA_HOME/webapps/ROOT $CATALINA_HOME/webapps/___ROOT
  2. ln -s $CATALINA_HOME/webapps/your_project $CATALINA_HOME/webapps/ROOT

Done.

Works for me.

Drus answered 24/11, 2011 at 16:24 Comment(0)
C
1

Ultimate way to change tomcat root application. Tested on Tomcat 7 and 8.

  1. Move to the tomcat webapps directory:

    Example on my machine: ~/stack/apache-tomcat/webapps

  2. Rename, replace or delete ROOT folder. My advice is renaming or create a copy for backup. Example rename ROOT to RENAMED_ROOT:

    mv ROOT RENAMED_ROOT

  3. Move war file with your application to tomcat webapps directory (its a directory where was old ROOT folder, on my machine: ~/stack/apache-tomcat/webapps)

War file must have a name ROOT.war. Rename your aplication if it's need: yourApplicationName.war -> ROOT.war

  1. Restart tomcat. After restart your application will be a root.
Celebration answered 26/4, 2018 at 9:44 Comment(0)
O
0

I'll look at my docs; there's a way of specifying a configuration to change the path of the root web application away from ROOT (or ROOT.war), but it seems to have changed between Tomcat 5 and 6.

Found this:

http://www.nabble.com/Re:-Tomcat-6-and-ROOT-application...-td20017401.html

So, it seems that changing the root path (in ROOT.xml) is possible, but a bit broken -- you need to move your WAR outside of the auto-deployment directory. Mind if I ask why just renaming your file to ROOT.war isn't a workable solution?

Opah answered 3/4, 2009 at 20:10 Comment(2)
Unfortunately changing the war file to ROOT.war is not an option for me. JacquesRosalba
a bit late, but... for me i can't change it because i am using a system created by a third party, who seems to have hard coded some locations to jar files.... so moving it to ROOT.war makes a problem, since the original application looks for its own jar under a directory which is in [originalapp.war]Exculpate
T
0

Not a very good solution but one way is to redirect from the ROOT app to YourWebApp. For this you need to modify the ROOT index.html.

<html>
    <head>
        <title>Redirecting to /YourWebApp</title>
    </head>
    <body onLoad="javascript:window.location='YourWebApp';">
    </body>
</html>

OR

<html>
    <head>
        <title>Redirecting to /YourWebApp</title>
        <meta http-equiv="refresh" content="0;url=YourWebApp" />
    </head>
    <body>
    </body>
</html>

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

Trug answered 10/6, 2013 at 15:56 Comment(0)
F
-4

In Tomcat 7 (under Windows server) I didn't add or edit anything to any configuration file. I just renamed the ROOT folder to something else and renamed my application folder to ROOT and it worked fine.

Finely answered 17/1, 2014 at 15:36 Comment(1)
This might be a good workaround, but Jaques' question is about how to change the default application; there may be reasons why he cannot change the contents of the ROOT folder.Cancan

© 2022 - 2024 — McMap. All rights reserved.