通过部署 helloworld.war 应用程序,可以了解服务器配置在 Liberty 中的更改方式。
开始之前
helloworld.war 应用程序使用简单的 servlet 在浏览器上显示一则消息。可以创建任何其他要显示的消息。Liberty 文档中未描述如何编写应用程序代码。
关于此任务
如果使用开发者工具将 Web 应用程序部署到 Liberty,那么会自动在 server.xml 文件中启用与应用程序相关的所有配置。但是,您也可以通过完成下列步骤来手动配置 server.xml 文件。
此示例使用 helloworld.war 应用程序,并且可以通过 http://localhost:9090/helloworld 来访问。在此示例中,已创建 Liberty 服务器实例,其缺省 HTTP 端口已更改为
9090,应用程序已部署至该端口。
过程
- 使用命令 server create hwserver 来创建服务器(名称为 hwserver)。
- 将 helloworld.war 应用程序复制到 /usr/servers/hwserver/apps 目录;在步骤 1 中使用 server create 命令创建了此目录。
- 在使用 server create 命令创建的 server.xml 文件中,如果要将服务器 hwserver 的缺省 HTTP 端口更改为 9090,请将属性值
httpPort="9080" 替换为 httpPort="9090":
<server description="new server">
<!-- Enable features -->
<featureManager>
<feature>jsp-2.2</feature>
</featureManager>
<httpEndpoint id="defaultHttpEndpoint"
host="localhost"
httpPort="9090"
httpsPort="9443" />
</server>
- 通过以下方式来更新 server.xml 以配置应用程序:
- 通过使用 webApplication 元素来定义应用程序:
<server description="Hello World Server">
<featureManager>
<feature>servlet-3.0</feature>
</featureManager>
<httpEndpoint id="defaultHttpEndpoint" host="*" httpPort="9090" />
<webApplication contextRoot="helloworld" location="helloworld.war" />
</server>
- 通过使用 application 元素来定义应用程序:
<server description="Hello World Server">
<featureManager>
<feature>servlet-3.0</feature>
</featureManager>
<httpEndpoint id="defaultHttpEndpoint" host="*" httpPort="9090" />
<application context-root="helloworld" type="war" id="helloworld"
location="helloworld.war" name="helloworld"/>
</server>
webApplication 元素可将相同子元素用作 application 元素,但 context-root 和 type 除外。这两个元素不会一起对 context-root
生效,如果 application 和 webApplication 元素定义相同 context-root,那么只会使用一个,并且显示错误。
context-root 属性指定所部署应用程序的入口点。按下列优先顺序确定所部署应用程序的入口点:
- server.xml 文件中的 context-root
- application.xml(如果是 EAR 应用程序)
- ibm-web-ext.xml(如果是 Web 应用程序)
- server.xml 文件中应用程序的 name(如果是 Web 应用程序)
- Manifest.MF(如果是 WAB 应用程序)
- 目录名称或与 Liberty 的 dropins 目录相对的文件名
注: 在应用程序服务器
server.xml 配置中,
application 元素可包含
context-root
标记。此
context-root 标记适合与标记
type="war" 一起使用。对于所有其他应用程序类型,
context-root
元素不起作用。
不能覆盖 EAR 应用程序或 EBA 应用程序的 context-root。不能对独立 WAR 文件或 webApplication 执行覆盖。
- 使用命令 server run hwserver 在前台启动服务器。
- 在 http://localhost:9090/helloworld 处测试应用程序。
- 可选: 如果不需要服务器,请停止服务器。