在项目中集成jetty server

为什么使用jetty

使用 tomcat 开发效率并不是太高,并且在eclipse有时两秒做更新,有时候又得手动去部署显得非常麻烦。折算我们可以使用 jetty server 由于 eclipse开发

http://www.eclipse.org/jetty/

当然eclipse 也开发相关的工具,你可以选择下载

这里我封装了 一个web开发中常用的例子,供大家使用

项目结构

Paste_Image.png

关键代码:

package jettyutils;

import java.io.IOException;
import java.util.Properties;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class JettyUtils {
    private static String PATH=null;
    private static String PORT="80";
    private static String VISITPATH=null;
    
    static{
        Properties pro = new Properties();
        try {
            pro.load(JettyUtils.class.getResourceAsStream("jetty.properties"));
            PATH = pro.getProperty("path");
            PORT = pro.getProperty("port");
            VISITPATH=pro.getProperty("viewpath");
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    private JettyUtils() {
    }
    public static void main(String[] args) throws Exception {
        Server server=new Server(Integer.parseInt(PORT));
        WebAppContext app=new WebAppContext();
        app.setDescriptor(PATH + "/WEB-INF/web.xml");
        app.setResourceBase(PATH);
        app.setContextPath(VISITPATH);
        server.setHandler(app);
        server.start();
        server.join();
    }

}

jetty.properties 文件

path=C:\\Users\\Administrator\\git\\jettydemo\\WebContent
port=80
viewpath=/hello

其中 的viewpath 是你项目访问的基础路径

结果

Paste_Image.png

这里我已经上传的码云,你可以选择性下载
地址:
http://git.oschina.net/dgwcode/jettydemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,987评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,080评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,898评论 25 709
  • 贾安姐姐家是邻村水泊。姐夫兄弟俩,姓刘,排行老二。刘老大家三个儿子,都很争气,生了四个孙子,两个孙女。而姐姐家俩儿...
    南良大维阅读 257评论 0 0
  • 双岗菜市 要搬了 也不远 就在边上 我去的时候 已现迁像 路过的路上 又见老经管 还是那样 招牌在雨水的冲刷下 居...
    一度一阅读 125评论 0 0