Eclispe下集成JFinal中jetty包作为开发环境

一、下载jetty包,

1.如果是gradle 或是maven项目地址在这,jetty-server包http://maven.oschina.net/index.html#nexus-search;quick~com.jfinal

二、引入JettyServer类(核心类),Scanner类(定时任务,热启动),工具类,PathKit,StringKit,FileKit(这些类都来自JFinal中源码)

测试工程目录

1.JettryServer类的实现

 package com.jetty; 
 import java.io.File;
 import java.io.IOException;
 import java.net.DatagramSocket;
 import java.net.ServerSocket; 
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.SessionManager;
 import org.eclipse.jetty.server.nio.SelectChannelConnector;
 import org.eclipse.jetty.server.session.HashSessionManager;
 import org.eclipse.jetty.server.session.SessionHandler;
 import org.eclipse.jetty.webapp.WebAppContext; 
 import com.kit.FileKit;
 import com.kit.PathKit;
 import com.kit.StringKit; 
 /**
  * JettyServer is used to config and start jetty web server.
  * Jetty version 8.1.8
  */
 public class JettyServer  {
 
  private String webAppDir;
  private int port;
  private String context;
  private int scanIntervalSeconds;
  private boolean running = false;
  private Server server;
  private WebAppContext webApp;
 
  public JettyServer(String webAppDir, int port, String context, int scanIntervalSeconds) {
   if (webAppDir == null)
    throw new IllegalStateException("Invalid webAppDir of web server: " + webAppDir);
   if (port < 0 || port > 65536)
    throw new IllegalArgumentException("Invalid port of web server: " + port);
   if (StringKit.isBlank(context))
    throw new IllegalStateException("Invalid context of web server: " + context);
 
   this.webAppDir = webAppDir;
   this.port = port;
   this.context = context;
   this.scanIntervalSeconds = scanIntervalSeconds;
  }
 
  public void start() {
   if (!running) {
    try {doStart();} catch (Exception e) {e.printStackTrace();}
    running = true;
   }
  }
 
  public void stop() {
   if (running) {
    try {server.stop();} catch (Exception e) {e.printStackTrace();}
    running = false;
   }
  }
 
  private void doStart() {
   if (!available(port))
    throw new IllegalStateException("port: " + port + " already in use!");
 
   deleteSessionData();
 
   //System.out.println("Starting JFinal " + Const.JFINAL_VERSION);
   server = new Server();
   SelectChannelConnector connector = new SelectChannelConnector();
   connector.setPort(port);
   server.addConnector(connector);
   webApp = new WebAppContext();
   webApp.setContextPath(context);
   webApp.setResourceBase(webAppDir); // webApp.setWar(webAppDir);
   webApp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
   webApp.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false"); // webApp.setInitParams(Collections.singletonMap("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false"));
   persistSession(webApp);
 
   server.setHandler(webApp);
 
   // configureScanner
   if (scanIntervalSeconds > 0) {
    Scanner scanner = new Scanner(PathKit.getRootClassPath(), scanIntervalSeconds) {
     public void onChange() {
      try {
       System.err.println("\nLoading changes ......");
       webApp.stop();
       webApp.start();
       System.err.println("Loading complete.");
      } catch (Exception e) {
       System.err.println("Error reconfiguring/restarting webapp after change in watched files");
       e.printStackTrace();
      }
     }
    };
    System.out.println("Starting scanner at interval of " + scanIntervalSeconds + " seconds.");
    scanner.start();
   }
 
   try {
    System.out.println("Starting web server on port: " + port);
    server.start();
    System.out.println("Starting Complete. Welcome To The Jetty World :)");
    server.join();
   } catch (Exception e) {
    e.printStackTrace();
    System.exit(100);
   }
   return;
  }
 
  private void deleteSessionData() {
   try {
    FileKit.delete(new File(getStoreDir()));
   }
   catch (Exception e) {
   }
  }
 
  private String getStoreDir() {
   String storeDir = PathKit.getWebRootPath() + "/../../session_data" + context;
   if ("\\".equals(File.separator))
    storeDir = storeDir.replaceAll("/", "\\\\");
   return storeDir;
  }
 
  private void persistSession(WebAppContext webApp) {
   String storeDir = getStoreDir();
 
   SessionManager sm = webApp.getSessionHandler().getSessionManager();
   if (sm instanceof HashSessionManager) {
    ((HashSessionManager)sm).setStoreDirectory(new File(storeDir));
    return ;
   }
 
   HashSessionManager hsm = new HashSessionManager();
   hsm.setStoreDirectory(new File(storeDir));
   SessionHandler sh = new SessionHandler();
   sh.setSessionManager(hsm);
   webApp.setSessionHandler(sh);
  }
 
  private static boolean available(int port) {
   if (port <= 0) {
    throw new IllegalArgumentException("Invalid start port: " + port);
   }
 
   ServerSocket ss = null;
   DatagramSocket ds = null;
   try {
    ss = new ServerSocket(port);
    ss.setReuseAddress(true);
    ds = new DatagramSocket(port);
    ds.setReuseAddress(true);
    return true;
   } catch (IOException e) {
   } finally {
    if (ds != null) {
     ds.close();
    }
 
    if (ss != null) {
     try {
      ss.close();
     } catch (IOException e) {
      // should not be thrown, just detect port available.
     }
    }
   }
   return false;
  }
 } 
 2.Sanner类的实现
 package com.jetty; 
 import java.io.File;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Timer;
 import java.util.TimerTask; 
 import com.kit.StringKit; 
 /**
  * Scanner.
  */
 public abstract class Scanner {
 
  private Timer timer;
  private TimerTask task;
  private File rootDir;
  private int interval;
  private boolean running = false;
 
  private final Map<String,TimeSize> preScan = new HashMap<String,TimeSize> ();
  private final Map<String,TimeSize> curScan = new HashMap<String,TimeSize> ();
 
  public Scanner(String rootDir, int interval) {
   if (StringKit.isBlank(rootDir))
    throw new IllegalArgumentException("The parameter rootDir can not be blank.");
   this.rootDir = new File(rootDir);
   if (!this.rootDir.isDirectory())
    throw new IllegalArgumentException("The directory " + rootDir + " is not exists.");
   if (interval <= 0)
    throw new IllegalArgumentException("The parameter interval must more than zero.");
   this.interval = interval;
  }
 
  public abstract void onChange();
 
  private void working() {
   scan(rootDir);
   compare();
 
   preScan.clear();
   preScan.putAll(curScan);
   curScan.clear();
  }
 
  private void scan(File file) {
   if (file == null || !file.exists())
    return ;
 
   if (file.isFile()) {
    try {
     curScan.put(file.getCanonicalPath(), new TimeSize(file.lastModified(),file.length()));
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   else if (file.isDirectory()) {
    File[] fs = file.listFiles();
    if (fs != null)
     for (File f : fs)
      scan(f);
   }
  }
 
  private void compare() {
   if (preScan.size() == 0)
    return;
 
   if (!preScan.equals(curScan))
    onChange();
  }
 
  public void start() {
   if (!running) {
    timer = new Timer("JFinal-Scanner", true);
    task = new TimerTask() {public void run() {working();}};
    timer.schedule(task, 1010L * interval, 1010L * interval);
    running = true;
   }
  }
 
  public void stop() {
   if (running) {
    timer.cancel();
    task.cancel();
    running = false;
   }
  }
 } 
 class TimeSize {
 
  final long time;
  final long size;
 
  public TimeSize(long time, long size) {
   this.time = time;
   this.size = size;
  }
 
  public int hashCode() {
   return (int)(time ^ size);
  }
 
  public boolean equals(Object o) {
   if (o instanceof TimeSize) {
    TimeSize ts = (TimeSize)o;
    return ts.time == this.time && ts.size == this.size;
   }
   return false;
  }
 
  public String toString() {
   return "[t=" + time + ", s=" + size + "]";
  }
 } 
 3,工具类  
 package com.kit; 
 import java.io.File; 
 /**
  * FileKit.
  */
 public class FileKit {
  public static void delete(File file) {
   if (file != null && file.exists()) {
    if (file.isFile()) {
     file.delete();
    }
    else if (file.isDirectory()) {
     File files[] = file.listFiles();
     for (int i=0; i<files.length; i++) {
      delete(files[i]);
     }
    }
    file.delete();
   }
  }
 } 
 package com.kit; 
 import java.io.File; 
 /**
  * new File("..\path\abc.txt") 中的三个方法获取路径的方法
  * 1: getPath() 获取相对路径,例如   ..\path\abc.txt
  * 2: getAbslutlyPath() 获取绝对路径,但可能包含 ".." 或 "." 字符,例如  D:\otherPath\..\path\abc.txt
  * 3: getCanonicalPath() 获取绝对路径,但不包含 ".." 或 "." 字符,例如  D:\path\abc.txt
  */
 public class PathKit {
 
  private static String webRootPath;
  private static String rootClassPath;
  public static String getRootClassPath() {
   if (rootClassPath == null) {
    try {
     String path = PathKit.class.getClassLoader().getResource("").toURI().getPath();
     rootClassPath = new File(path).getAbsolutePath();
    }
    catch (Exception e) {
     String path = PathKit.class.getClassLoader().getResource("").getPath();
     rootClassPath = new File(path).getAbsolutePath();
    }
   }
   return rootClassPath;
  }
 
  public static String getWebRootPath() {
   if (webRootPath == null)
    webRootPath = detectWebRootPath();;
   return webRootPath;
  }
 
  public static void setWebRootPath(String webRootPath) {
   if (webRootPath == null)
    return ;
 
   if (webRootPath.endsWith(File.separator))
    webRootPath = webRootPath.substring(0, webRootPath.length() - 1);
   PathKit.webRootPath = webRootPath;
  }
 
  private static String detectWebRootPath() {
   try {
    String path = PathKit.class.getResource("/").toURI().getPath();
    return new File(path).getParentFile().getParentFile().getCanonicalPath();
   } catch (Exception e) {
    throw new RuntimeException(e);
   }
  } 
 } 
 package com.kit; 
 /**
  * StringKit.
  */
 public class StringKit {
 
  /**
   * 首字母变小写
   */
  public static String firstCharToLowerCase(String str) {
   Character firstChar = str.charAt(0);
   String tail = str.substring(1);
   str = Character.toLowerCase(firstChar) + tail;
   return str;
  }
 
  /**
   * 首字母变大写
   */
  public static String firstCharToUpperCase(String str) {
   Character firstChar = str.charAt(0);
   String tail = str.substring(1);
   str = Character.toUpperCase(firstChar) + tail;
   return str;
  }
 
  /**
   * 字符串为 null 或者为  "" 时返回 true
   */
  public static boolean isBlank(String str) {
   return str == null || "".equals(str.trim()) ? true : false;
  }
 
  /**
   * 字符串不为 null 而且不为  "" 时返回 true
   */
  public static boolean notBlank(String str) {
   return str == null || "".equals(str.trim()) ? false : true;
  }
 
  public static boolean notBlank(String... strings) {
   if (strings == null)
    return false;
   for (String str : strings)
    if (str == null || "".equals(str.trim()))
     return false;
   return true;
  }
 
  public static boolean notNull(Object... paras) {
   if (paras == null)
    return false;
   for (Object obj : paras)
    if (obj == null)
     return false;
   return true;
  }
 } 

三,准备工作完成,见证奇迹的时刻来了

1,新建一个Servlet

 package com; 
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse; 
 @SuppressWarnings("serial")
 public class FisrtServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) {
   doPost(request, response);
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response) {
   System.out.println("jetty我来了");
  }
 } 

2.web.xml

 <?xml version="1.0" encoding="ISO-8859-1"?> 
 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0" metadata-complete="true">
  <servlet>
   <servlet-name>servletTest</servlet-name>
   <servlet-class>com.FisrtServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>servletTest</servlet-name>
   <url-pattern>/test</url-pattern>
  </servlet-mapping>
 
 </web-app> 

3.启动类

 package com; 
 import com.jetty.JettyServer; 
 public class JettyTest {
     public static void main(String[] args) {
      new JettyServer("WebRoot", 80, "/", 5).start();;
  }
 } 

测试启动,结果如下

localhost/test访问后

需要demo可发邮箱451671299@qq.com,这里找不到上传文件地方,好吧,我太菜了

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏小白鼠

SpringBoot几个注解MockMvcWireMockSwagger2@JsonViewHibernate Validator异常处理拦截方式上传下载异步处理RESTSpring Security

只有特定名称或者类型的Bean(通过@ConditionalOnMissingBean修饰)不存在于BeanFactory中时才创建某个Bean

2953
来自专栏码匠的流水账

聊聊reactor-netty的PoolResources的两种模式

本文主要研究下reactor-netty的PoolResources的两种模式elastic及fixed。

2131
来自专栏猿天地

smjdbctemplate基于Spring JdbcTemplate的升级版

GitHub地址:https://github.com/yinjihuan/smjdbctemplate

2962
来自专栏菩提树下的杨过

java 利用JAX-RS快速开发RESTful 服务

JAX-RS(Java API for RESTful Web Services)同样也是JSR的一部分,详细规范定义见 https://jcp.org/en/...

3247
来自专栏数据之美

Pig、Hive 自定义输入输出分隔符以及Map、Array嵌套分隔符冲突问题

PIG中输入输出分隔符默认是制表符\t,而到了hive中,默认变成了八进制的\001, 也就是ASCII: ctrl - A Oct   Dec   Hex  ...

2805
来自专栏Hongten

spring开发_Spring+Hibernate_HibernateDaoSupport

http://www.cnblogs.com/hongten/archive/2012/03/15/java_spring_hibernate_hibernat...

1101
来自专栏清晨我上码

基于session的国际化实现方式

1302
来自专栏云计算与大数据

研发:How To Install Python 3 on CentOS 7

Python is a versatile programming language that can be used for many different p...

792
来自专栏精讲JAVA

接口方法上的注解无法被 @Aspect 声明的切面拦截的原因分析

在Spring中使用MyBatis的Mapper接口自动生成时,用一个自定义的注解标记在Mapper接口的方法中,再利用@Aspect定义一个切面,拦截这个注解...

4234
来自专栏精讲JAVA

接口方法上的注解无法被 @Aspect 声明的切面拦截的原因分析

在Spring中使用MyBatis的Mapper接口自动生成时,用一个自定义的注解标记在Mapper接口的方法中,再利用@Aspect定义一个切面,拦截这个注解...

3372

扫码关注云+社区

领取腾讯云代金券