zoukankan      html  css  js  c++  java
  • 嵌入式tomcat使用

    需要引入的maven依赖

    <!--属性文件 -->
        <properties>
            <tomcat.version>8.5.24</tomcat.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
                <version>${tomcat.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jasper</artifactId>
                <version>${tomcat.version}</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <finalName>Mytomcat</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
    
                <!-- maven打包插件-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>hope.redheart.wx.StartTomcat</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>

     启动方法

    public static void main(String[] args) throws ServletException, LifecycleException {
    
            int port = 80;
    
            if (args.length == 1) {
                port = Integer.parseInt(args[0]);
            }
    
            // 获取项目绝对路径
            File webappPath = new File(System.getProperty("user.dir"));
    
            // 创建tomat容器
            Tomcat tomcat = new Tomcat();
    
            // 设置端口
            tomcat.setPort(port);
    
            // 设置webapp目录
            StandardContext sct = (StandardContext) tomcat.addWebapp("/", webappPath.getAbsolutePath());
    
            // web项目路径
            WebResourceRoot resourceRoot = new StandardRoot(sct);
    
            // 相当于webapps中的一个项目文件夹
            DirResourceSet dirResourceSet = new DirResourceSet();
    
            // java字节码文件路径
            dirResourceSet.setBase(new File("target/classes").getAbsolutePath());
            // tomcat的默认写法 /WEB-INF/classes web项目的默认
            dirResourceSet.setWebAppMount("/WEB-INF/classes");
    
            // 这个项目文件夹的上一级目录,在tomcat中就是webapps
            dirResourceSet.setRoot(resourceRoot);
    
            dirResourceSet.setInternalPath("/");
    
            // 相当于把项目设置到webapp中
            resourceRoot.addPreResources(dirResourceSet);
    
            sct.setReloadable(false);
    
            // 一个web项目
            sct.setResources(resourceRoot);
    
            tomcat.start();
            System.out.println("Tomcat已在本地" + port + "端口启动!");
    
            // 方法阻塞,否则main方法运行完毕就退出,tomcat也就关闭了
            tomcat.getServer().await();
        }
  • 相关阅读:
    第七周——Linux内核如何装载和启动一个可执行程序
    第十八章读书笔记
    第三章读书笔记
    第六周——分析Linux内核创建一个新进程的过程
    第五章读书笔记
    Linux内核分析——分析system_call中断处理过程
    第一二章读书笔记
    20145217《网络对抗》 Web安全基础实践
    20145217《网络对抗》web基础
    20145217《网络对抗》 MSF基础应用
  • 原文地址:https://www.cnblogs.com/yjp372928571/p/11738944.html
Copyright © 2011-2022 走看看