配置好参数,只需在Booter类中调用 EmbbedTomcat.main(args); 就可以启动。 maven中也需要配置相应插件。

import java.io.File;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.startup.Tomcat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EmbbedTomcat {

  private static final Logger logger = LoggerFactory.getLogger(EmbbedTomcat.class);

  private String hostname = "localhost";
  private int port = 8089;
  private String webAppDir = "webapp";
  private String contextPath = "/";
  private String URIEncoding = "UTF-8";

  private String baseDir = ".";

  // absolute pathname, a relative pathname, or a URL
  private String appBase = ".";

  private Tomcat tomcat = null;

  public String getHostname() {
    return hostname;
  }

  public void setHostname(String hostname) {
    this.hostname = hostname;
  }

  public int getPort() {
    return port;
  }

  public void setPort(int port) {
    this.port = port;
  }

  public String getWebAppDir() {
    return webAppDir;
  }

  public void setWebAppDir(String webAppDir) {
    this.webAppDir = webAppDir;
  }

  public String getContextPath() {
    return contextPath;
  }

  public void setContextPath(String contextPath) {
    this.contextPath = contextPath;
  }

  public String getBaseDir() {
    return baseDir;
  }

  public void setBaseDir(String baseDir) {
    this.baseDir = baseDir;
  }

  public String getAppBase() {
    return appBase;
  }

  public void setAppBase(String appBase) {
    this.appBase = appBase;
  }

  public void setURIEncoding(String uRIEncoding) {
    URIEncoding = uRIEncoding;
  }

  public void start() throws Exception {

    tomcat = new Tomcat();

    tomcat.setPort(port);
    tomcat.setHostname(hostname);
    tomcat.setSilent(false);
    tomcat.setBaseDir(baseDir);
    tomcat.getConnector().setURIEncoding(URIEncoding);
    tomcat.getConnector().setEnableLookups(false);

    // webapps
    tomcat.getHost().setAppBase(System.getProperty("user.dir") + File.separator + appBase);

    StandardServer server = (StandardServer) tomcat.getServer();
    AprLifecycleListener listener = new AprLifecycleListener();
    server.addLifecycleListener(listener);

    // ROOT
    org.apache.catalina.Context ctx = tomcat.addWebapp(contextPath, webAppDir);
    String contextPath = this.getClass().getResource("/").getPath() + "context.xml";
    File contextFile = new File(contextPath);
    ctx.setConfigFile(contextFile.toURI().toURL());

    tomcat.enableNaming();
    tomcat.start();

    // add shutdown hook to stop server
    Runtime.getRuntime().addShutdownHook(new Thread() {
      public void run() {
        try {
          tomcat.stop();
        } catch (LifecycleException e) {
          logger.error("failed to stop tomcat.", e);
        }
      }
    });

    tomcat.getServer().await();
  }

  public static void main(String[] args) {

    int port = 0;
    String appBase = null;
    String contextPath = null;
    String webAppDir = null;
    String baseDir = null;
    String URIEncoding = null;
    for (String arg : args) {
      if (arg.startsWith("-httpPort")) {
        port = Integer.parseInt(arg.substring(arg.indexOf("=") + 1));
      }
      if (arg.startsWith("-appBase")) {
        appBase = arg.substring(arg.indexOf("=") + 1);
      }
      if (arg.startsWith("-contextPath")) {
        contextPath = arg.substring(arg.indexOf("=") + 1);
      }
      if (arg.startsWith("-webAppDir")) {
        webAppDir = arg.substring(arg.indexOf("=") + 1);
      }
      if (arg.startsWith("-baseDir")) {
        baseDir = arg.substring(arg.indexOf("=") + 1);
      }
      if (arg.startsWith("-URIEncoding")) {
        URIEncoding = arg.substring(arg.indexOf("=") + 1);
      }
    }

    EmbbedTomcat tomcat = new EmbbedTomcat();
    if (port > 0) {
      tomcat.setPort(port);
    }
    if (appBase != null && appBase.length() > 0) {
      tomcat.setAppBase(appBase);
    }
    if (contextPath != null && contextPath.length() > 0) {
      tomcat.setContextPath(contextPath);
    }
    if (webAppDir != null && webAppDir.length() > 0) {
      tomcat.setWebAppDir(webAppDir);
    }
    if (baseDir != null && baseDir.length() > 0) {
      tomcat.setBaseDir(baseDir);
    }
    if (URIEncoding != null && URIEncoding.length() > 0) {
      tomcat.setURIEncoding(URIEncoding);
    }

    try {
      tomcat.start();
    } catch (Exception e) {
      logger.error("Server Start Error: ", e);
      System.exit(-1);
    }

  }

}

Tomcat 代码方式启动的更多相关文章

  1. 以服务方式启动tomcat无法访问NFS共享盘

    用startup.bat方式启动tomcat,程序的可以访问NFS共享盘的文件.但用 1).以服务的方式启动tomcat 2).或者用windows的任务计划去执行startup.bat的方式启动to ...

  2. tomcat三种启动不同的启动方式

    Linux下tomcat服务的启动.关闭与错误跟踪,通常通过以下几种方式启动关闭tomcat服务: 切换到tomcat主目录下的bin目录 1. 启动tomcat服务 方式一:直接启动 ./start ...

  3. windows系统bat方式启动tomcat出现java.lang.OutOfmemoryError:PermGen Space 错误

    1.问题情景: 在部署项目时,将两个应用部署到同一个tomcat下,通过startup.bat启动服务时,控制台出现出现java.lang.OutOfmemoryError:PermGen Space ...

  4. idea中使用tomcat 方式启动spring boot项目

    Spring boot 的main 入口启动方式相信都会用,直接运行main直接就启动了,但是往往这种方式并不是最佳的启动方式,比如运维的层面更希望调整tomcat的调优参数,而只使用嵌入启动方式很难 ...

  5. C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法的示例与源码下载带详细注释-源码代码-注册表方式-启动目录快捷方式

    C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法的示例与源码下载带详细注释-源码代码-注册表方式-启动目录快捷方式 C#实现自动启动的方法-两种方法 源码下载地址: ...

  6. Spring Boot移除内嵌Tomcat,使用非web方式启动

    前言:当我们使用Spring Boot编写了一个批处理应用程序,该程序只是用于后台跑批数据,此时不需要内嵌的tomcat,简化启动方式使用非web方式启动项目,步骤如下: 1.在pom.xml文件中去 ...

  7. 如何在开机时让Tomcat以进程的方式启动

    一. 安装tomcat服务 1. 打开cmd命令窗口,进入到"tomcat安装路径/bin"目录下,运行"service.bat install"命令,安装to ...

  8. 关于eclipse tomcat 无法启动(8080,8005,8009端口被占用)的解决方法,附 eclipse tomcat 与 tomcat 并存方式

    eclipse 在编译运行时 新建的tomcat连接始终为stopped状态,描述为8080,8005,8009端口被占用. 这是因为在装完tomcat后,tomcat服务已启动,而eclipse仅仅 ...

  9. 部署war包后,新增tomcat服务器,启动tomcat服务器报错解决方法

    导入Maven工程后,新增tomcat服务器,启动服务器后,报如下错误: 使用http访问页面的时候报如下错误: 经过百度后,有一种方法可以解决: 在eclipse tomcat无法启动,无法访问to ...

随机推荐

  1. JMeter生成HTML性能报告

    有时候我们写性能报告的时候需要一些性能分布图,JMeter是可以生成HTML性能报告的 一.准备工作 1:jmeter3.0版本之后开始支持动态生成测试报表 2:jdk版本1.7以上 3:需要jmx脚 ...

  2. 洛谷 [P3355] 骑士共存问题

    二分图求最大独立点集 本问题在二分图中已处理过,此处用dinic写了一遍 #include <iostream> #include <cstdio> #include < ...

  3. BZOJ 1415: [Noi2005]聪聪和可可 [DP 概率]

    传送门 题意:小兔子乖乖~~~ 题意·真:无向图吗,聪抓可,每个时间聪先走可后走,聪一次可以走两步,朝着里可最近且点编号最小的方向:可一次只一步,等概率走向相邻的点或不走 求聪抓住可的期望时间 和游走 ...

  4. 【视频编解码·学习笔记】7. 熵编码算法:基础知识 & 哈夫曼编码

    一.熵编码概念: 熵越大越混乱 信息学中的熵: 用于度量消息的平均信息量,和信息的不确定性 越是随机的.前后不相关的信息,其熵越高 信源编码定理: 说明了香农熵越信源符号概率之间的关系 信息的熵为信源 ...

  5. 01 Mybatis 的配置和使用

    一.Mybatis 是什么 MyBatis 是一个支持普通SQL查询.存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索封装.MyB ...

  6. CentOS 7 搭建基于携程Apollo(阿波罗)配置中心单机模式

    Apollo(阿波罗)是携程框架部门研发的配置管理平台,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性.服务端基于Spring Boot ...

  7. gitlab安装备忘录

    [root@linux ~]# curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh ...

  8. Navicat的使用

    Navicat的使用 navicat作为一种数据库的操作工具,在工作中使用的频率很高.相比phpMyAdmin而言,无论是从界面操作的易用性上,还是外观上,抑或是IP的配置上都有着很大的突出优势.ph ...

  9. Filezilla Server 出现Error, could not connect to server解决办法

    打开任务管理器:Win+R:services.msc找到Filezilla Server并启动服务

  10. window 下生成NodeJs(v8.9.3) 的 VS2015 解决方案node.sln

    window 下生成NodeJs(v8.9.3) 的 VS2015 解决方案node.sln 使用步骤 也可以参照 github: https://github.com/nodejs/node/blo ...