Tomcat嵌入式启动

本文详细介绍了如何进行Tomcat的嵌入式启动,包括导入必要的jar包,创建Maven Web项目,设置webserver.properties配置文件,编写启动主方法以及核心代码配置,最后演示了如何访问启动后的Web页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、导入jar包

    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-core</artifactId>
      <version>8.5.13</version>
    </dependency>
    <!--主要获取配置属性信息-->
    <dependency>
      <groupId>commons-configuration</groupId>
      <artifactId>commons-configuration</artifactId>
      <version>1.10</version>
    </dependency>
    <!--避免启动出错-->
    <dependency>
      <groupId>tomcat</groupId>
      <artifactId>jasper-runtime</artifactId>
      <version>5.5.23</version>
    </dependency>

2、利用Maven建立一个web项目,项目结构如图:
这里写图片描述
3、项目属性文件webserver.properties

webserver.hostname=localhost
webserver.port=8081
webserver.webappDir=src/main/webapp
webserver.maxPostSize=0
webserver.maxThreads=10
webserver.acceptCount=10

4、启动主方法

public class TestBoot {
    public static void main(String[] args) {
        WebServce.getWebserce().start();
    }
}

5、主要代码,一些配置信息

public class WebServce {
    //设置主机或ip
    private String hostname="localhost";
    //设置端口,默认的端口,主要看配置属性
    private int port=8899;
    //
    private String webappDir="webapp";
    //设置 连接时的一些参数
    private int maxPostSize=0;
    private int maxThreads=200;
    private int acceptCount=100;

    //tomcat引用
    private Tomcat tomcat;

    public WebServce(){}


    //获取属性信息
   protected  void loadProperties()throws IOException{
    PropertiesConfiguration conf=new PropertiesConfiguration();
    try{
        //提供文件名
        conf.load("webserver.properties");
    }catch (ConfigurationException ce){

    }

    //根据配置文件修改初始值
    //第二个参数是默认值,当第一个为空时,使用默认值
    this.hostname=conf.getString("webserver.hostname","localhost");
    this.port=conf.getInt("webserver.port",8899);
    this.webappDir=conf.getString("webserver.webappDir","webapp");
    this.maxPostSize=conf.getInt("webserver.maxPostSize",0);
    this.maxThreads=conf.getInt("webserver.maxThreads",200);
    this.acceptCount=conf.getInt("webserver.acceptCount",100);

    }

    //启动
    public void start(){
        try{
            //加载配置
            this.loadProperties();
            //tomcat实例
            this.tomcat=new Tomcat();
            this.tomcat.setPort(this.port);
            this.tomcat.setHostname(this.hostname);
            //tomcat存储自身信息,保存在项目目录下
            this.tomcat.setBaseDir(".");

            this.configServer(this.tomcat.getServer());
            this.tomcat.getEngine();
            this.configHost(this.tomcat.getHost());
            this.configConnector(this.tomcat.getConnector());
            //第一个参数上下文路径contextPath,第二个参数docBase
            this.tomcat.addWebapp("", System.getProperty("user.dir")+ File.separator+this.webappDir);

            //这种方式也行
          //  this.tomcat.getHost().setAppBase(System.getProperty("user.dir")+ File.separator+".");
          //  this.tomcat.addWebapp("",this.webappDir);

            this.tomcat.start();

            this.tomcat.getServer().await();

        }catch(Exception e){

        }
    }

    private void configHost(Host host) {
        //user.dir  用户的当前工作目录
        host.setAppBase(System.getProperty("user.dir"));
    }

    private void configServer(Server server) {
        AprLifecycleListener listener = new AprLifecycleListener();
        server.addLifecycleListener(listener);
    }

    //设置连接属性
    private void configConnector(Connector connector) {
        connector.setURIEncoding("UTF-8");
        connector.setMaxPostSize(this.maxPostSize);
        connector.setAttribute("maxThreads", Integer.valueOf(this.maxThreads));
        connector.setAttribute("acceptCount", Integer.valueOf(this.acceptCount));
        connector.setAttribute("disableUploadTimeout", Boolean.valueOf(true));
        connector.setAttribute("enableLookups", Boolean.valueOf(false));
    }


   public static WebServce getWebserce(){
       return new WebServce();
   }



}

6、访问页面
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值