在WAR-File中定义Servlet上下文 [英] Define Servlet Context in WAR-File

查看:106
本文介绍了在WAR-File中定义Servlet上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该怎么说? Tomcat在给出我的WAR-File时使用特定的上下文路径?

How can I tell e.g. Tomcat to use a specific context path when given my WAR-File?

示例:
我有一个由maven build创建的war文件以及由此产生的名称文件很长。
所以我不希望tomcat管理器应用程序使用war的文件名作为上下文。

Example: I have a war file created by maven build and the resulting name of the file is rather long. So I do not want the tomcat manager application to use the filename of the war as the context.

在META-INF中提供context.xml没有产生预期的结果

Supplying a context.xml in META-INF did not produce the desired results

我还在路径属性的文档中找到了这个上下文

I also found this in the documentation for the path attribute of Context:


除非在server.xml中静态定义Context,否则不能设置此字段的值,因为它将从用于.xml上下文文件或docBase的文件名中推断出来。

The value of this field must not be set except when statically defining a Context in server.xml, as it will be inferred from the filenames used for either the .xml context file or the docBase.

所以它似乎没有是告诉应用程序服务器我的WAR路径应该是什么的正确方法。

So it does not seem to be the right way to tell the application-server what the path for my WAR should be.

还有其他提示吗?

推荐答案

上下文容器



    $ CATALINA_BASE / conf / [enginename] / [hostname] / 目录中的单个文件(扩展名为.xml)。 文件名(减去.xml扩展名)将用作上下文路径。可以使用#来定义多级上下文路径,例如, foo #bar.xml,用于/ foo / bar的上下文路径。可以使用名为ROOT.xml的文件来定义默认Web应用程序。
  • 仅当 $ CATALINA_BASE / conf中的应用程序不存在上下文文件时/ [enginename] / [hostname] / ,位于应用程序文件中 /META-INF/context.xml 的单个文件中。 如果Web应用程序打包为WAR,则 /META-INF/context.xml 将被复制到 $ CATALINA_BASE / conf / [ enginename] / [hostname] / 并重命名以匹配应用程序的上下文路径。一旦这个文件存在,如果在主机的appBase中放置了一个带有较新 /META-INF/context.xml 的新WAR,它就不会被替换。

  • In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. foo#bar.xml for a context path of /foo/bar. The default web application may be defined by using a file called ROOT.xml.
  • Only if a context file does not exist for the application in the $CATALINA_BASE/conf/[enginename]/[hostname]/, in an individual file at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.

因此,捆绑 META-INF / context.xml ,文件被重命名为WAR的名称,无论上下文中定义的路径是什么,该名称都成为上下文路径元素。

So, when you bundle a META-INF/context.xml, the file gets renamed to the name of the WAR and this name becomes the context path, regardless of any path defined in the Context element.

我在这里看到两个选项:

I see thus two options here:


  1. 将生成的战争的名称设置为较短的名称(我建议使用< finalName> 超过< warName> 已弃用的AFAIK):

  1. Either set the name of the generated war to a shorter name (I suggest using <finalName> over <warName> which is deprecated AFAIK):

<project>
  ...
  <build>
    <finalName>mycontext</finalName>
    ...
  </build>
  ...
</project>


  • 或者使用 maven-tomcat-plugin 并在插件配置中设置上下文路径:

  • Or use the maven-tomcat-plugin for the deployment and set the context path in the plugin configuration:

    <project>
      ...
      <build>
        ...
        <plugins>
          ...
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <configuration>
              <path>/mycontext</path>
            </configuration>
          </plugin>
          ...
        </plugins>
        ...
      </build>
      ...
    </project>
    


  • 这篇关于在WAR-File中定义Servlet上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆