首页  

tomcat8 嵌入式 servlet 实例     所属分类 tomcat 浏览量 741
tomcat8 嵌入式启动

http://127.0.0.1:8086/hello

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

public class Main {

    public static void main(String[] args) throws Exception {
        Tomcat tomcat = new Tomcat();
        tomcat.setHostname("dyyx");
        tomcat.setPort(8086);
        // tomcat.setBaseDir("/tmp/");
        String contextPath = "";

        StandardContext context = new StandardContext();
        context.setPath(contextPath);
        context.addLifecycleListener(new Tomcat.FixContextListener());
        tomcat.getHost().addChild(context);

        tomcat.addServlet(contextPath, "hello", new HelloServlet());
        context.addServletMapping("/hello", "hello");

        tomcat.start();

        tomcat.getServer().await();
        
    }
}


public class HelloServlet 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 { resp.getWriter().write("hello tomcat," + LocalDateTime.now()); } }
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>8.5.0</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-logging-juli</artifactId> <version>8.5.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency>
完整代码 https://gitee.com/dyyx/demos/tree/master/tomcatdemo

上一篇     下一篇
mysql的读已提交和可重复读

springboot基础配置

mysql redo undo binlog

tomcat8 连接器

Tomcat NIO 处理机制

tomcat8的后台线程