Embed Tomcat Java(内嵌tomcat启动简述)

简单记录一下内部tomcat启动

maven pom.xml

    
        
        
            org.apache.tomcat.embed
            tomcat-embed-core
            8.5.28
        
        
            org.apache.tomcat
            tomcat-jasper
            8.5.28
        

        
        
            org.springframework
            spring-web
            5.0.4.RELEASE
            compile
        
        
            org.springframework
            spring-webmvc
            5.0.4.RELEASE
            compile
        
    
    
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.5.1
                
                    1.8
                    1.8
                    UTF-8
                
            
        
    

tomcat启动类

/**
 * 
 * @author Programming is an art from.
 * @Description: TODO
 */
public class TomcatStart {

    public static int TOMCAT_PORT = 8080;
    public static String TOMCAT_HOSTNAME = "127.0.0.1";
    public static String WEBAPP_PATH = "src/main";
    public static String WEBINF_CLASSES = "/WEB-INF/classes";
    public static String CLASS_PATH = "target/classes";
    public static String INTERNAL_PATH = "/";
    
    public static void main(String[] args) throws ServletException, LifecycleException {
        TomcatStart.run();
    }
    
    public static void run() throws ServletException, LifecycleException {
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(TomcatStart.TOMCAT_PORT);
        tomcat.setHostname(TomcatStart.TOMCAT_HOSTNAME);
        tomcat.setBaseDir("."); // tomcat 信息保存在项目下
        
        /*
         * https://www.cnblogs.com/ChenD/p/10061008.html
         */
        StandardContext myCtx = (StandardContext) tomcat
            .addWebapp("/access", System.getProperty("user.dir") + File.separator + TomcatStart.WEBAPP_PATH);
        /*
         * true时:相关classes | jar 修改时,会重新加载资源,不过资源消耗很大
         * autoDeploy 与这个很相似,tomcat自带的热部署不是特别可靠,效率也不高。生产环境不建议开启。
         * 相关文档:
         * http://www.blogjava.net/wangxinsh55/archive/2011/05/31/351449.html
         */
        myCtx.setReloadable(false);
        // 上下文监听器
        myCtx.addLifecycleListener(new AprLifecycleListener());
        
        /*String webAppMount = System.getProperty("user.dir") + File.separator + TomcatStart.CLASS_PATH;
        WebResourceRoot root = new StandardRoot(myCtx);
        root.addPreResources(
            new DirResourceSet(root, TomcatStart.WEBINF_CLASSES, webAppMount, TomcatStart.INTERNAL_PATH));*/
        
        // 注册servlet
        tomcat.addServlet("/access", "demoServlet", new DemoServlet());
        // servlet mapping
        myCtx.addServletMappingDecoded("/demo.do", "demoServlet");
        tomcat.start();
        tomcat.getServer().await();
    }
    
}

注意! contextPath不要设置为 /
否则会报错, 错误信息为以下。

警告: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/] does not meet these criteria and has been changed to []
Exception in thread "main" java.lang.NullPointerException
    at org.apache.catalina.startup.Tomcat.addServlet(Tomcat.java:341)
    at org.apache.catalina.startup.Tomcat.addServlet(Tomcat.java:325)
    at cn.learn.config.TomcatStart.run(TomcatStart.java:63)
    at cn.learn.config.TomcatStart.main(TomcatStart.java:33)

servlet class

/**
 * 
 * @author Programming is an art from.
 * @Description: TODO
 */
public class DemoServlet extends HttpServlet{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().print("access success!!!");
    }


}

启动main方法,访问成功喽!
http://127.0.0.1:8080/access/demo.do

你可能感兴趣的:(Embed Tomcat Java(内嵌tomcat启动简述))