透過部署 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 檔中,以 httpPort="9090" 取代屬性值 httpPort="9080",以便將伺服器 hwserver 的預設 HTTP 埠變更為 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>
除了 context-root 和 type,webApplication 元素所用的子元素,可以與 application 元素相同。就
context-root 來說,這兩個元素不會一起運作,如果 application 和
webApplication 元素都定義了相同的 context-root,只會使用其中一個,且會顯示錯誤。
context-root 屬性會指定已部署之應用程式的進入點。已部署之應用程式的進入點,取決於下列優先順序:
- server.xml 檔中的 context-root
- application.xml(EAR 應用程式)
- ibm-web-ext.xml(Web 應用程式)
註: 在
server.xml 檔中的
application 元素下,
web-ext 元素的
context-root 屬性。
- server.xml 檔中應用程式的 name(Web 應用程式)
- Manifest.MF(WAB 應用程式)
- 相對於 Liberty drop-ins 目錄的目錄名稱或檔名
註: 在應用程式伺服器的
server.xml 配置中,
application 元素可以包含
context-root 標籤。這個
context-root 標籤適合與
type="war" 標籤併用。如果是其他所有的應用程式類型,
context-root 元素就沒有作用。
如果是 EAR 應用程式或 EBA 應用程式,不可能置換 context-root。唯一可執行置換的是獨立式 WAR 檔或 webApplication。
- 利用 server run hwserver 指令,在前景啟動伺服器。
- 在 http://localhost:9090/helloworld 測試應用程式。
- 選擇性的: 如果不需要伺服器,請停止伺服器。