在命令行上部署一个简单的应用程序

  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!