SpringMVC项目使用Jetty

简介

在 java web 项目开发过程中,如果遇到需要频繁重启服务器的情况,可以使用 jetty 来代替 tomcat,因为使用 jetty 的话,我们可以直接在项目中添加一个类,在其 main 方法中添加启动的方法即可,免去了部署与发布的过程,项目的启动比 tomcat 要快的多

实现步骤

  1. 首先要使用的 jar 包是 jetty-server-8.1.8.jar,将其添加到项目目录下即可

  2. 在启动类中添加如下方法, 参数依次是: 端口号,服务器地址,项目 WebContent 根目录路径,Web.xml文件路径,项目名

public static void start(int portNumber, String connectorAddress, String resourceBase, String webFilePath, String projectName) {
    try {
        Server server = new Server();
        Connector connector = new SelectChannelConnector();
        connector.setHost(connectorAddress);
        connector.setPort(portNumber);
        server.setConnectors(new Connector[] {
            connector
        });
        server.setStopAtShutdown(true);
        WebAppContext webAppContext = new WebAppContext(resourceBase, "/" + projectName);
        webAppContext.setDescriptor(resourceBase + webFilePath);
        webAppContext.setResourceBase(resourceBase);
        webAppContext.setContextPath("/" + projectName);
        webAppContext.setDisplayName(projectName);
        webAppContext.setClassLoader(Thread.currentThread().getContextClassLoader());
        webAppContext.setConfigurationDiscovered(true);
        webAppContext.setParentLoaderPriority(true);
        server.setHandler(webAppContext);
        server.start();
        //server.join();
        System.out.println("http://" + connectorAddress + ":" + portNumber + "/" + projectName);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  1. 启动的时候,可以像下面这样调用
public static void main(String[] args) {
    startProject(8080, "localhost", "WebContent", "/WEB-INF/web.xml", "AutoMationTool");
}

特别注意的是要在 spring-servlet.xml 文件中添加如下配置

<!--Only For jetty -->
<mvc:default-servlet-handler/>

否则,可能会造成静态页面无法成功调用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值