• 使用maven-Jetty9-plugin插件运行第一个Servlet


    写在前面

    公司的 Spring MVC 使用的是 Jetty,所以想了解一下 jetty-maven-plugin 的插件使用,以下是一个简单的 demo。

    项目地址

    git clone https://gitee.com/kendoziyu/code-servlet-parent.git
    

    其中,maven-jetty9-plugin-servlet 是本文的项目。

    引用 maven-jetty-plugin

    首先,要澄清一下,本文的标题是有一定的“误导性”的,实际上只有 maven-jetty-plugin 插件,而没有 maven-jetty9-plugin 这款插件的。
    第一步,我们添加 maven-jetty-plugin 插件到你的 pom.xml

    <plugin>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-maven-plugin</artifactId>
      <version>9.4.33.v20201020</version>
    </plugin>
    

    创建一个 Servlet

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class HelloJettyServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            PrintWriter writer = resp.getWriter();
            writer.write("<h1> Hello, Jetty! </h1>");
            writer.flush();
        }
    }
    

    因为你在编写代码时,可能找不到 Servlet API,所以你需要在你的 pom.xml 引入 javax.servlet-api 依赖

    <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
    </dependency>
    

    创建 webapp 目录

    创建 src/main/webapp 目录,并在 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_3_1.xsd"
             version="3.1">
        <servlet>
            <servlet-name>helloServlet</servlet-name>
            <servlet-class>HelloJettyServlet</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>helloServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    

    运行程序

    我们可以在项目根目录使用 mvn jetty:run 来启动项目。也可以在 IDEA 中使用 Add Configuration...

    jetty:run 用来启动 jetty 项目。在 Run/Debug Configuration 中进行设置:

    端口占用/换个端口

    我在运行时遇到了端口占用的问题,你也有可能有把端口改为 80 的需求,那么 maven-jetty-plugin 怎么修改端口呢?

    网上比较常见的一种配置方法:

    <plugin>
          <groupId>org.mortbay.jetty</groupId>
          <artifactId>maven-jetty-plugin</artifactId>
          <version>6.1.22</version>
          <configuration>
                <connectors>
                      <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>10001</port>
                      </connector>
                </connectors>
          </configuration>
    </plugin>
    

    但是,上面这种方法对 Jetty 9 不起作用!!!我们查阅了一些资料,发现以下配置是可行的:

    <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.4.33.v20201020</version>
        <configuration>
            <httpConnector>
                <port>80</port>
            </httpConnector>
            <scanIntervalSeconds>10</scanIntervalSeconds>
        </configuration>
    </plugin>
    

    使用的是 <httpConnector> 标签。如果未指定,Jetty 将创建一个侦听端口 8080 的ServerConnector实例。

    参考文档

    官方文档 jetty-maven-plugin
    jetty-maven-plugin 9版本如何设置端口

  • 相关阅读:
    September 17th 2016 Week 38th Saturday
    【2016-09-16】UbuntuServer14.04或更高版本安装问题记录
    September 16th 2016 Week 38th Friday
    September 11th 2016 Week 38th Sunday
    September 12th 2016 Week 38th Monday
    September 10th 2016 Week 37th Saturday
    September 9th 2016 Week 37th Friday
    c++暂停
    八皇后问题
    ( 转转)Android初级开发第九讲--Intent最全用法(打开文件跳转页面等)
  • 原文地址:https://www.cnblogs.com/kendoziyu/p/first-servlet-project-using-maven-Jetty9-plugin.html
Copyright © 2020-2023  润新知