How to set the context path of a web application in Tomcat 7.0

后端 未结 13 1347
傲寒
傲寒 2020-11-21 23:34

I know that I can rename my webapp (or it\'s WAR file) to ROOT but this is a terrible way to do it, IMHO. Now I checked out the tomcat doc & it says

相关标签:
13条回答
  • 2020-11-21 23:53

    I faced this problem for one month,Putting context tag inside server.xml is not safe it affect context elements deploying for all other host ,for big apps it take connection errors also not good isolation for example you may access other sites by folder name domain2.com/domain1Folder !! also database session connections loaded twice ! the other way is put ROOT.xml file that has context tag with full path such :

     <Context path="" docBase="/var/lib/tomcat7/webapps/ROOT" />
    

    in conf/catalina/webappsfoldername and deploy war file as ROOT.war inside webappsfoldername and also specify host such

     <Host name="domianname"  appBase="webapps2" unpackWARs="true"  autoDeploy="true"  xmlValidation="false" xmlNamespaceAware="false" >
    
            <Logger className="org.apache.catalina.logger.FileLogger"
                   directory="logs"  prefix="localhost_log." suffix=".txt"
              timestamp="true"/>
    </Host>
    

    In this approach also for same type apps user sessions has not good isolation ! you may inside app1 if app1 same as app2 you may after login by server side session automatically can login to app2 ?! So you have to keep users session in client side cache and not with jsessionid ! we may change engine name from localhost to solve it. but let say playing with tomcat need more time than play with other cats!

    0 讨论(0)
  • 2020-11-21 23:53

    Simplest and flexible solution is below: Inside ${Tomcat_home}/config/server.xml

    Change the autoDeploy="false" deployOnStartup="false" under Host element like below This is must.

    <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="false" deployOnStartup="false">
    

    Add below line under Host element.

    <Context path="" docBase="ServletInAction.war"  reloadable="true">
                <WatchedResource>WEB-INF/web.xml</WatchedResource>
            </Context>
    

    With the above approach we can add as many applications under webapps with different context path names.

    0 讨论(0)
  • 2020-11-21 23:56

    For me both answers worked.

    1. Adding a file called ROOT.xml in /conf/Catalina/localhost/
    <Context
        docBase="/tmp/wars/hpong"
      path=""
      reloadable="true"
    />
    
    1. Adding entry in server.xml
    <Service name="Catalina2">
        <Connector port="8070" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8743" />
        <Engine name="Catalina2" defaultHost="localhost">
            <Host name="localhost"
                unpackWARs="true" autoDeploy="true">
                <Context path="" docBase="/tmp/wars/hpong"  reloadable="true">
                    <WatchedResource>WEB-INF/web.xml</WatchedResource>
                </Context>
          </Host>
        </Engine>
    </Service>
    

    Note: when you declare docBase under context then ignore appBase at Host.

    1. However I have preferred converting my war name as ROOT.war and place it under webapps. So now unmatched url requests from other wars(contextpaths) will land into this war. This is better way to handle ROOT ("/**") context path.

    The second option is (double) loading the wars from Webapps folder as well. Also it only needs uncompressed war folder which is a headache.

    0 讨论(0)
  • 2020-11-21 23:57

    It's not recommended to update the server configuration like server.xml or ROOT.xml.

    You can put a context.xml configuration file under your web-application META-INF directory, with the context path setting included. This will override the default server setting?

    i.e.:

    <Context docBase="yourAppName" path="/yourAppPath" reloadable="true">
    
    0 讨论(0)
  • 2020-11-22 00:02

    <Context docBase="yourAppName" path="" reloadable="true">

    go to Tomcat server.xml file and set path blank

    0 讨论(0)
  • 2020-11-22 00:02

    The below trick worked for me.

    1) Comment/delete the below configuration from server.xml file (inside conf folder) of tomcat.

    2) Delete the existing ROOT folder (If any) residing inside tomcat webapps folder. And rename your war (e.g: test.war ) file to ROOT.war.

    Remember that while renaming war file to ROOT.war "ROOT" should be in caps.

    Limitation: You can deploy only one application inside one tomcat instance.

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