【问题标题】:Set Tomcat Default Context Path设置 Tomcat 默认上下文路径
【发布时间】:2012-11-30 06:40:31
【问题描述】:

在我的context.xml 文件中,我将以下内容设置为:<Context antiJARLocking="true" path="/" />

当我从 NetBeans 运行我的项目时,它可以正常工作并转到 http://localhost:8080/login。然后,当我清理并构建并进入 Tomcat 管理器并部署 war 文件时,由于某种原因,它转到了http://localhost:8080/appName/login。我不确定它为什么要添加上下文路径,或者它甚至从哪里获取它,但是当我手动部署它时它会这样做。当我直接从 Netbeans 运行项目时,它不会。在我直接从 NetBeans 运行它之后,如果我转到 Tomcat 管理器,那么它会显示部署在上下文路径 / 下的应用程序,这是正确的。当我手动部署 .war 时,它会部署在上下文路径 /appName

【问题讨论】:

    标签: java tomcat deployment war context.xml


    【解决方案1】:

    听起来您正在将您的战争文件构建为“appName.war”。这就是tomcat将它部署在“/appName”下的原因。

    如果您希望您的应用程序可以在 / 访问,您可以将您的 war 文件重命名为 ROOT.war 并将其放入 /webapps,它应该可以通过 http://localhost:8080/ 访问

    【讨论】:

    • 也许有一种自动的方法可以做到这一点?等等maven插件?
    【解决方案2】:

    某些应用程序的编写方式是更改上下文路径需要更改代码。如果您遇到这种情况,这里是另一种让您的服务器默认为特定上下文的方法:

    第 1 步)将其放入 [tomcat]/conf/web.xml

    <welcome-file-list>      
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    

    第 2 步)将此文件添加到您的 ROOT webapp 文件夹中,其他所有内容保持不变: index.html(用于根应用程序)。使用这种 javascript 方法而不是普通重定向,允许“重定向”工作并保持相同的 url 参数。

    <!doctype html>
    <html>
    <head>
    
    <script language="JavaScript">
        document.location.href = "/mycontext" + document.location.search;
    </script>
    
    </head>
    
    </html>
    

    【讨论】:

      【解决方案3】:

      使用这个 Maven 插件:

      <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
          <configuration>
              <path>/</path>
              <warFile>${project.build.directory}/fileName.war</warFile>
          </configuration>
      </plugin>
      

      删除 META-INF 文件夹中的 context.xml 文件。

      使用以下命令运行您的项目:mvn tomcat7:run

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-24
        • 2019-08-22
        • 2022-11-25
        • 2011-01-02
        • 2012-01-21
        • 2021-07-30
        相关资源
        最近更新 更多