首页 文章

Tomcat 8.5上下文路径无法正常工作

提问于
浏览
0

我有一个名为cat_tiger.war的应用程序,它被部署为localhost:8080 / cat_tiger,但我想将上下文路径更改为localhost:8080 / cat / lion / . 我已将META-INF / context.xml文件添加为

<?xml version="1.0" encoding="UTF-8"?> <Context copyXML="true" docBase="cat_tiger" path="/cat/lion"/>

和server.xml文件

<Host name="localhost" appBase="webapps" copyXML="true" deployXML="true" unpackWARs="true" autoDeploy="true">

但它仍然部署为localhost:8080 / cat_tiger /

关于还有什么需要改变的想法?

编辑:[1] Tomcat 8.5.3

[2] 04-Sep-2018 13:50:41.830 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor [/opt/tomcat/conf/Catalina/localhost/cat_tiger.xml] has finished in [2,750] ms ... ... 04-Sep-2018 13:50:39.070 WARNING [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDescriptor A docBase [/opt/tomcat/webapps/cat_tiger] inside the host appBase has been specified, and will be ignored

[3]我正在通过Netbeans进行部署,但最终必须部署在Docker容器中 .

1 回答

  • 0

    我没有't understand why you are having problems, but I'使用NetBeans 8.2(文件>新项目...> Java Web> Web应用程序)创建了Web项目,并成功更改了上下文,因此如果我提供项目的详细信息,您可以确定哪里存在重大差异 .

    1 cat_tiger\web\META-INF\context.xml

    我的文件与您的文件相同:

    <?xml version="1.0" encoding="UTF-8"?>
    <Context copyXML="true" docBase="cat_tiger" path="/cat/lion"/>
    

    [2]运行输出

    选择项目节点,右键单击并从弹出菜单中选择 Run 以在Tomcat 8.5上运行:

    ant -f D:\\NB82\\cat_tiger -Dnb.internal.action.name=run -Ddirectory.deployment.supported=true -DforceRedeploy=false -Dnb.wait.for.caches=true -Dbrowser.context=D:\\NB82\\cat_tiger -Duser.properties.file=C:\\Users\\johndoe\\AppData\\Roaming\\NetBeans\\8.2\\build.properties run
    init:
    deps-module-jar:
    deps-ear-jar:
    deps-jar:
    library-inclusion-in-archive:
    library-inclusion-in-manifest:
    compile:
    compile-jsps:
    In-place deployment at D:\NB82\cat_tiger\build\web
    Deployment is in progress...
    deploy?config=file%3A%2FC%3A%2FUsers%2Fjohndoe%2FAppData%2FLocal%2FTemp%2Fcontext7953615149857268018.xml&path=/cat/lion
    OK - Deployed application at context path [/cat/lion]
    Start is in progress...
    start?path=/cat/lion
    OK - Started application at context path [/cat/lion]
    run-deploy:
    Browsing: http://localhost:8080/cat/lion
    run-display-browser:
    run:
    BUILD SUCCESSFUL (total time: 0 seconds)
    

    实际上,您可能不需要运行应用程序来查看问题;只需从弹出菜单中选择 Deploy 而不是 Run . 这是我从 Deploy 获得的输出:

    ant -f D:\\NB82\\cat_tiger -Dnb.internal.action.name=redeploy -Ddirectory.deployment.supported=true -DforceRedeploy=true -Dnb.wait.for.caches=true -Dbrowser.context=D:\\NB82\\cat_tiger -Duser.properties.file=C:\\Users\\johndoe\\AppData\\Roaming\\NetBeans\\8.2\\build.properties run-deploy
    init:
    deps-module-jar:
    deps-ear-jar:
    deps-jar:
    library-inclusion-in-archive:
    library-inclusion-in-manifest:
    compile:
    compile-jsps:
    Undeploying ...
    undeploy?path=/cat_tiger
    OK - Undeployed application at context path [/cat_tiger]
    In-place deployment at D:\NB82\cat_tiger\build\web
    Deployment is in progress...
    deploy?config=file%3A%2FC%3A%2FUsers%2Fjohndoe%2FAppData%2FLocal%2FTemp%2Fcontext5063723197082921373.xml&path=/cat/lion
    OK - Deployed application at context path [/cat/lion]
    Start is in progress...
    start?path=/cat/lion
    OK - Started application at context path [/cat/lion]
    run-deploy:
    BUILD SUCCESSFUL (total time: 0 seconds)
    

    [3] Tomcat日志

    以下是Tomcat日志中的部署详细信息,您可以在其中看到我的部署正确显示了上下文,而您的部署则没有:

    05-Sep-2018 23:09:09.321 INFO [http-nio-8080-exec-6] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying configuration descriptor [C:\apache-tomcat-8.5.20\conf\Catalina\localhost\cat#lion.xml]
    05-Sep-2018 23:09:09.334 INFO [http-nio-8080-exec-6] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor [C:\apache-tomcat-8.5.20\conf\Catalina\localhost\cat#lion.xml] has finished in [13] ms
    05-Sep-2018 23:09:09.338 INFO [http-nio-8080-exec-5] org.apache.catalina.util.LifecycleBase.start The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/cat/lion]] after start() had already been called. The second call will be ignored.
    

    [4] C:\apache-tomcat-8.5.20\conf\Catalina\localhost\cat#lion.xml

    这是Tomcat在部署应用程序时创建的文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <Context copyXML="true" docBase="D:\NB82\cat_tiger\build\web" path="/cat/lion"/>
    

    请注意 docBase 包含绝对路径 .

    [5] server.xml

    这里's the entire content. I'm使用Tomcat 8.5 . 请注意 <host> 元素与您的元素略有不同,但是当我将 copyXML="true" deployXML="true" 添加到 <host> 元素(因此看起来像你的元素)时,一切都继续正常工作:

    <?xml version="1.0" encoding="UTF-8"?>
    <Server port="8005" shutdown="SHUTDOWN">
        <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
        <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
        <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" relaxedQueryChars="[]|{}^+\`&quot;&lt;&gt;"/>
            <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 &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/>
                    <Context docBase="C:\apache-tomcat-8.5.20\wtpwebapps\PlusServlet" path="/PlusServlet" reloadable="true" source="org.eclipse.jst.jee.server:PlusServlet"/>
                </Host>
            </Engine>
        </Service>
    </Server>
    

    Updated 9/6/2018:

    忽略上面给出的答案!即使它恰好适用于我的配置,但在Tomcat文档中明确禁止该方法 .

    具体而言,不应在放置在应用程序 META-INF 目录中的 context.xml 文件的 <Context> 中指定 path . 从Common Attributes section of the Tomcat 8.5 documentationpath 描述的Context容器:

    只有在server.xml中静态定义Context时,才能使用此属性 . 在所有其他情况下,将从用于.xml上下文文件或docBase的文件名推断出该路径 . 即使在server.xml中静态定义Context,也不能设置此属性,除非docBase不在Host的appBase下,或者deployOnStartup和autoDeploy都是false . 如果未遵循此规则,则可能会导致双重部署 .

相关问题