内嵌tomcat快速入门


1.依赖
<!--Java语言操作tomcat -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.16</version>
</dependency>

<!-- tomcat对jsp支持 -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>8.5.16</version>
</dependency>
2.新建一个IndexServlet 集成 HttpServlet,重写init,doGet,doPost
    
/**
 * Project Name:mk-springboot <br>
 * Package Name:com.suns.servlet <br>
 *
 * @author mk <br>
 * Date:2019-3-14 17:04 <br>
 */

package com.suns.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: IndexServlet <br>
 * Description:  <br>
 * @author mk
 * @Date 2019-3-14 17:04 <br>
 * @version
 */
public class IndexServlet extends HttpServlet {

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

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(this.getClass()+" doPost");
        resp.getWriter().print("this is a embed tomcat");
    }

    @Override
    public void init() throws ServletException {
        System.out.println(this.getClass()+" init");
    }
}
View Code
  3.新建tomcat启动类MkTomcat, 使用main方法运行
/**
 * Project Name:mk-springboot <br>
 * Package Name:com.suns.servlet <br>
 *
 * @author mk <br>
 * Date:2019-3-14 17:07 <br>
 */

package com.suns.servlet;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;

/**
 * ClassName: MkTomcat <br>
 * Description:  <br>
 * @author mk
 * @Date 2019-3-14 17:07 <br>
 * @version
 */
public class MkTomcat {


    private static int PORT = 8080;
    private static String CONTEX_PATH = "/mk";
    private static String SERVLET_NAME = "indexServlet";


    public static void main(String[] args) throws LifecycleException {
        // 创建tomcat服务器
        Tomcat tomcatServer = new Tomcat();
        // 指定端口号
        tomcatServer.setPort(PORT);
        // 是否设置自动部署
        tomcatServer.getHost().setAutoDeploy(false);
        // 创建上下文
        StandardContext standardContex = new StandardContext();
        standardContex.setPath(CONTEX_PATH);
        // 监听上下文
        standardContex.addLifecycleListener(new Tomcat.FixContextListener());
        // tomcat容器添加standardContex
        tomcatServer.getHost().addChild(standardContex);

        // 创建Servlet
        tomcatServer.addServlet(CONTEX_PATH, SERVLET_NAME, new IndexServlet());
        // servleturl映射
        standardContex.addServletMappingDecoded("/index", SERVLET_NAME);
        tomcatServer.start();
        System.out.println("tomcat服务器启动成功..");
        // 异步进行接收请求
        tomcatServer.getServer().await();
    }
}
View Code
   4.访问入口:http://localhost:8080/mk/index   就可以看到效果

posted on 2019-03-14 17:18  sgyi06  阅读(314)  评论(0编辑  收藏  举报

导航