使用Maven构建Java Servlet应用 | AI生成和翻译

Home 2025.02

以下是使用 Maven 构建项目并通过 Liberty Maven 插件部署,在 WebSphere Liberty Profile (WLP) 上创建和部署简单 Java Servlet 应用程序的完整指南。本示例将创建一个基础 Servlet,在访问时返回 “Hello World!”,使用 Maven 将其构建为 WAR 文件并部署到 WLP。我们假设您已设置好启用了 servlet-4.0 功能的 WLP,因为这提供了必要的 Servlet 支持。


分步指南

1. 创建 Maven 项目结构

首先设置标准的 Maven Web 应用程序结构。您可以手动创建或使用 Maven 生成。

2. 编写 Servlet 代码

src/main/java/com/example/ 目录下创建名为 HelloServlet.java 的文件,内容如下:

package com.example;

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

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/plain");
        resp.getWriter().write("Hello World!");
    }
}

3. 创建 web.xml 部署描述符

src/main/webapp/WEB-INF/ 目录下创建名为 web.xml 的文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.example.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

4. 配置 Maven pom.xml

SimpleServletApp/ 目录下创建或更新 pom.xml,内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>simple-servlet-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- Servlet API(由 WLP 提供) -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Maven WAR 插件用于构建 WAR 文件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
                <configuration>
                    <finalName>myapp</finalName>
                </configuration>
            </plugin>
            <!-- Liberty Maven 插件用于部署 -->
            <plugin>
                <groupId>io.openliberty.tools</groupId>
                <artifactId>liberty-maven-plugin</artifactId>
                <version>3.3.4</version>
                <configuration>
                    <installDirectory>/opt/ibm/wlp</installDirectory>
                    <serverName>myServer</serverName>
                    <appsDirectory>dropins</appsDirectory>
                    <looseApplication>false</looseApplication>
                    <stripVersion>true</stripVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

5. 构建项目

SimpleServletApp/ 目录下,使用 Maven 构建 WAR 文件:

mvn clean package

6. 在 WebSphere Liberty 上部署和运行

确保您的 Liberty 服务器 (myServer) 已设置并启用了 servlet-4.0 功能。检查您的 server.xml 是否包含:

<featureManager>
    <feature>servlet-4.0</feature>
</featureManager>

使用 Liberty Maven 插件部署并运行应用程序:

mvn liberty:run

7. 访问应用程序

打开浏览器并导航到:

http://localhost:9080/myapp/hello

8. 停止服务器

由于 mvn liberty:run 在前台运行服务器,请在终端中按 Ctrl+C 停止它。


注意事项


总结

本指南演示了如何:

  1. 设置一个包含简单 Servlet (HelloServlet.java) 和 web.xml 的 Maven 项目。
  2. 使用 Servlet API、Maven WAR 插件和 Liberty Maven 插件配置 pom.xml
  3. 使用 mvn clean package 将应用程序构建为 myapp.war
  4. 使用 mvn liberty:run 在 WLP 上部署和运行它。
  5. http://localhost:9080/myapp/hello 访问 “Hello World!”。

这提供了一种基于 Maven 的简化方法,用于在 WebSphere Liberty Profile 上开发和部署 Servlet 应用程序。



Back Donate