How to change the ROOT application?

后端 未结 13 1338
终归单人心
终归单人心 2020-11-22 16:16

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?

相关标签:
13条回答
  • 2020-11-22 16:28

    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.

    0 讨论(0)
  • 2020-11-22 16:28

    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?

    0 讨论(0)
  • 2020-11-22 16:33

    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.
    0 讨论(0)
  • 2020-11-22 16:34

    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

    0 讨论(0)
  • 2020-11-22 16:40

    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

    0 讨论(0)
  • 2020-11-22 16:40

    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

    0 讨论(0)
提交回复
热议问题