【问题标题】:Default web app in tomcatTomcat中的默认Web应用程序
【发布时间】:2011-08-04 01:43:42
【问题描述】:

这几天我一直在寻找解决方案。 我的问题是..我必须在 tomcat 中部署一个项目并将其设置为默认 webapp。我所做的是复制我的战争文件并将其放在 tomcat/webapps 文件夹中。启动tomcat并将端口更改为默认值。现在我可以在http://localhost/myapp 访问我的应用程序。我想要在http://localhost 看到我的应用程序。我怎样才能做到这一点?

【问题讨论】:

标签: web-applications tomcat default


【解决方案1】:

在 webapps/ROOT 下创建 index.html。

<html>

<head>
    <meta http-equiv="refresh" content=0;URL="http://localhost/myapp">
</head>

<body>
</body>

</html>

【讨论】:

  • 您是否介意添加一些信息,说明您为什么认为这是解决问题的最佳方法? (而不是服务器端重定向)。我不认为这是一个答案,在我投票之前,我想听听你的推理。
  • 这仅适用于作为浏览器的客户端(即加载和解析 HTML)。例如,如果您使用 Python urllib2httplib2requests 库,则不会。
  • 同上,这只是一个网页,不是真正的重定向
【解决方案2】:

我可以通过在 server.xml 文件的 Host 下添加带有 path="" 属性的 "Context" 元素来做到这一点。

            <Context path="" docBase="/usr/local/tomcat/mywebapps/myapplication">
                <WatchedResource>WEB-INF/web.xml</WatchedResource>
            </Context>

注意:我使用的是 Tomcat 7。此外,我必须将我的 webapp 移动到与默认 webapps 文件夹不同的位置。我还删除了 tomcat 提供的默认 webapps。但即使不删除它们,此解决方案也能正常工作。

这里是我的 server.xml 的内容供参考。希望这会有所帮助。

<?xml version='1.0' encoding='utf-8'?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
    license agreements. See the NOTICE file distributed with this work for additional 
    information regarding copyright ownership. The ASF licenses this file to 
    You under the Apache License, Version 2.0 (the "License"); you may not use 
    this file except in compliance with the License. You may obtain a copy of 
    the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
    by applicable law or agreed to in writing, software distributed under the 
    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
    OF ANY KIND, either express or implied. See the License for the specific 
    language governing permissions and limitations under the License. -->
<!-- Note: A "Server" is not itself a "Container", so you may not define 
    subcomponents such as "Valves" at this level. Documentation at /docs/config/server.html -->
<Server port="8005" shutdown="SHUTDOWN">
    <!-- Security listener. Documentation at /docs/config/listeners.html <Listener 
        className="org.apache.catalina.security.SecurityListener" /> -->
    <!--APR library loader. Documentation at /docs/apr.html -->
    <Listener className="org.apache.catalina.core.AprLifecycleListener"
        SSLEngine="on" />
    <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
    <Listener className="org.apache.catalina.core.JasperListener" />
    <!-- Prevent memory leaks due to use of particular java/javax APIs -->
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

    <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html -->
    <GlobalNamingResources>
        <!-- Editable user database that can also be used by UserDatabaseRealm 
            to authenticate users -->
        <Resource name="UserDatabase" auth="Container"
            type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved"
            factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
            pathname="conf/tomcat-users.xml" />
    </GlobalNamingResources>

    <!-- A "Service" is a collection of one or more "Connectors" that share 
        a single "Container" Note: A "Service" is not itself a "Container", so you 
        may not define subcomponents such as "Valves" at this level. Documentation 
        at /docs/config/service.html -->
    <Service name="Catalina">

        <!--The connectors can use a shared executor, you can define one or more 
            named thread pools -->
        <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" 
            minSpareThreads="4"/> -->


        <!-- A "Connector" represents an endpoint by which requests are received 
            and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html 
            (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) 
            Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8080 -->
        <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000"
            redirectPort="443" />
        <!-- A "Connector" using the shared thread pool -->
        <!-- <Connector executor="tomcatThreadPool" port="80" protocol="HTTP/1.1" 
            connectionTimeout="20000" redirectPort="443" /> -->
        <!-- Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the 
            JSSE configuration, when using APR, the connector should be using the OpenSSL 
            style configuration described in the APR documentation -->
        <!-- <Connector port="443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" 
            scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> -->

        <!-- Define an AJP 1.3 Connector on port 8009 -->
        <Connector port="8009" protocol="AJP/1.3" redirectPort="443" />


        <!-- An Engine represents the entry point (within Catalina) that processes 
            every request. The Engine implementation for Tomcat stand alone analyzes 
            the HTTP headers included with the request, and passes them on to the appropriate 
            Host (virtual host). Documentation at /docs/config/engine.html -->

        <!-- You should set jvmRoute to support load-balancing via AJP ie : <Engine 
            name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> -->
        <Engine name="Catalina" defaultHost="localhost">

            <!--For clustering, please take a look at documentation at: /docs/cluster-howto.html 
                (simple how to) /docs/config/cluster.html (reference documentation) -->
            <!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> -->

            <!-- Use the LockOutRealm to prevent attempts to guess user passwords 
                via a brute-force attack -->
            <Realm className="org.apache.catalina.realm.LockOutRealm">
                <!-- This Realm uses the UserDatabase configured in the global JNDI resources 
                    under the key "UserDatabase". Any edits that are performed against this UserDatabase 
                    are immediately available for use by the Realm. -->
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                    resourceName="UserDatabase" />
            </Realm>

            <Host name="localhost">

                <!-- SingleSignOn valve, share authentication between web applications 
                    Documentation at: /docs/config/valve.html -->
                <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" 
                    /> -->

                <!-- Access log processes all example. Documentation at: /docs/config/valve.html 
                    Note: The pattern used is equivalent to using pattern="common" -->
                <Valve className="org.apache.catalina.valves.AccessLogValve"
                    directory="logs" prefix="localhost_access_log." suffix=".txt"
                    pattern="%h %l %u %t &quot;%r&quot; %s %b" resolveHosts="false" />

                <Context path="" docBase="/usr/local/tomcat/mywebapps/myapplication">
                    <WatchedResource>WEB-INF/web.xml</WatchedResource>
                </Context>

            </Host>
        </Engine>
    </Service>
</Server>

【讨论】:

  • 这种方法不会在 webapps 下部署两倍的应用程序吗?来自official Tomcat doc:“使用自动部署时,XML Context 文件定义的 docBase 应该在 appBase 目录之外。如果不是这样,部署 Web 应用程序可能会遇到困难,或者应用程序可能会被部署两次。 "
【解决方案3】:

您可以将您的 webapp 重命名为 ROOT(tomcat 的默认应用程序),或者您可以使用可以处理域名和重定向的 web 服务器(如 apache httpserver),并在那里配置您的站点。

第一种方法不是很优雅,但是快速高效。

【讨论】:

    【解决方案4】:

    删除Tomcat/webapps/ROOT 文件夹(如果有),将您的WAR 文件重命名为ROOT.war 并重新启动。

    【讨论】:

    • 我做了,但我得到了 apache tomcat 主页。
    • 那么您正在修改或访问错误的 Tomcat 实例。你用的是 IDE 还是什么的?
    • 是的,我使用netbeans IDE来部署项目。
    • 您应该在项目属性中将上下文名称设置为空字符串或/。由于我不使用 Netbeans,我无法详细说明如何/在哪里。无论如何,将 IDE 置于考虑之外时,答案是有效的,并且您完全按照问题中提到的方式进行操作:复制 WAR 并放置在 tomcat/webapps 中。
    • 是的,我做到了,我不再使用 netbeans。我关闭了 netbeans,只使用了 war 文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2012-12-28
    • 2013-07-17
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 2013-01-04
    相关资源
    最近更新 更多