java内嵌tomcat并启动

java内嵌tomcat并启动

创建工程

创建maven工程ee-tomat-embed

pom.xml添加依赖如下


  UTF-8


  
  
    org.apache.tomcat.embed
    tomcat-embed-core
    8.5.39
  

  
  
    org.apache.tomcat.embed
    tomcat-embed-el
    8.5.39
  

  
  
    org.apache.tomcat.embed
    tomcat-embed-jasper
    8.5.39
  

  
  
    javax.servlet
    javax.servlet-api
    3.1.0
  

在工程目录下创建一个webapp目录,在webapp新增一个index.html文件index.html文件中随便写点代码

创建启动类

推荐添加Listen后再添加WebAPP

import java.io.File;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.startup.Tomcat;

import com.billrobot.tomcat.servlet.HelloServlet;

public class TomcatConfig {
  public static void main(String[] args) throws LifecycleException {
    long start = System.currentTimeMillis();
    // 设置端口
    int port = 8888;
    Tomcat tomcat = new Tomcat();
    tomcat.setPort(port);

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

    // 设置contextPath和路径
    String contextPath = "/ee-tomat-embed";
    String docBase = new File("webapp").getAbsolutePath();
    Context context = tomcat.addWebapp(contextPath, docBase);
    System.out.println("添加contextPath和docBase是:" + contextPath + "==>" + docBase);

    // add servlet
    // Context context = tomcat.addContext(contextPath, baseDir);
    String servletName = "hello";
    String servletMapping = "/hello";
    tomcat.addServlet(contextPath, servletName, new HelloServlet());
    context.addServletMappingDecoded(servletMapping, servletName);
    // 启动tomcat
    tomcat.start();
    long end = System.currentTimeMillis();
    System.out.println("启动完成,共使用了:" + (end - start) + "ms");
    // 进入监听状态,如果不进入监听状态,启动tomat后就会关闭tomcat
    tomcat.getServer().await();
  }
}

启动完成后访问静态文件

访问静态文件

访问servlet

打包部署

在pom.xml中添加



  
  
    local
    
      
        
          src/main/resources/local
        
      
    

    
      true
    
    
      local
    
  

  
  
    dev
    
      
        
          src/main/resources/dev
        
      
    

    
      dev
    
  

  
  
    tests
    
      
        
          src/main/resources/tests
        
      
    
    
      tests
    
  

  
  
    prod
    
      
        
          src/main/resources/prod
        
      
    

    
      prod
    
  




  
  
    
      src/main/resources
      
        local/**
        dev/**
        tests/**
        prod/**
      
      true
    

  

  

    
    
    
      org.apache.maven.plugins
      maven-jar-plugin
      2.6
      
        
          /*.*
        
      
    

    
    
      org.apache.maven.plugins
      maven-assembly-plugin
      3.1.0
      
        
          make-assembly
          package
          
            single
          

          
            
            ${project.build.finalName}
            
            true
            
            true
            
            
              src/main/assembly/package.xml
            
            
            ${project.build.directory}/
          
        
      
    
  

执行下面的命令打包

mvn clean install -Dmaven.test.skip=true -Pdev

完成的包ee-tomat-embed-1.0-release-dev.tar.gz,仅仅5.93 MB,依赖的jar包

ecj-3.12.3.jar
ee-tomat-embed-1.0.jar
javax.servlet-api-3.1.0.jar
tomcat-annotations-api-8.5.39.jar
tomcat-embed-core-8.5.39.jar
tomcat-embed-el-8.5.39.jar
tomcat-embed-jasper-8.5.39.jar

前台启动

在Windwos前台启动使用下面的命令,向classpath中添加config

java -Xverify:none -cp .\lib\*;config com.billrobot.tomcat.TomcatConfig

启动成功

java内嵌tomcat并启动_第1张图片

 

参考链接

Java org.apache.catalina.startup.Tomcat 代码实例

https://www.helplib.com/Java_API_Classes/article_67063

jfinal 内嵌tomcat,打包可运行jar

https://www.jianshu.com/p/6f16c593a4ac

项目地址

https://gitee.com/eblly/jfinalCase

官网API

http://tomcat.apache.org/tomcat-8.5-doc/api/org/apache/catalina/startup/package-summary.html

Java org.apache.catalina.startup.Tomcat 代码实例

https://www.helplib.com/Java_API_Classes/article_67063

jfinal 内嵌tomcat,打包可运行jar

https://www.jianshu.com/p/6f16c593a4ac

项目地址

https://gitee.com/eblly/jfinalCase

官网API

http://tomcat.apache.org/tomcat-8.5-doc/api/org/apache/catalina/startup/package-summary.html

你可能感兴趣的:(tomcat,java内嵌tomcat并启动,tomcat-embed)