在命令列上部署一個簡單的應用程式

  1. 建立一個簡單的 servlet:
package web.example;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/*")
public class HelloServlet extends HttpServlet 
{           
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.getWriter().println("Hello world!");
    }
}
  1. 將應用程式打包到 Web 存檔(.war)中:
helloapp.war
+- META-INF\
+- WEB-INF\
   +- web\example\HelloServlet.class
  1. 將應用程式新增到 Liberty 伺服器:
$>  mv helloapp.war $WLP_INSTALL_DIR/usr/servers/myServer/apps/
  1. 配置 server.xml 以瞭解應用程式並啟用 Servlet 3.1 技術:
$>  cat $WLP_INSTALL_DIR/usr/servers/myServer/server.xml

<server>
  <featureManager>
    <feature>servlet-3.1</feature>
  </featureManager>
  
  <application location="helloapp.war"/>    
</server>
  1. 啟動伺服器:
$>  server start myServer
Starting server myServer
Server myServer started with process ID 1234.
  1. 檢查 console.log 以驗證應用程式是否已啟動,以及在以下位置找到它的 URL:
$>  tail $WLP_INSTALL_DIR/usr/servers/myServer/logs/console.log
...
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://localhost:9080/helloapp/
[AUDIT   ] CWWKZ0001I: Application helloapp started in 0.272 seconds.
  1. 在 Web 瀏覽器中,按照 console.log 中的說明轉到 URL http://localhost:9080/helloapp/。你應該看到來自 servlet 的訊息:
Hello world!