简单记录一下内部tomcat启动

maven pom.xml

	<dependencies>
<!-- embed tomcat dependency -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.28</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>8.5.28</version>
</dependency> <!-- spring dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.4.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 添加编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

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启动简述)的更多相关文章

  1. Spring Boot移除内嵌Tomcat,使用非web方式启动

    前言:当我们使用Spring Boot编写了一个批处理应用程序,该程序只是用于后台跑批数据,此时不需要内嵌的tomcat,简化启动方式使用非web方式启动项目,步骤如下: 1.在pom.xml文件中去 ...

  2. 基于内嵌Tomcat的应用开发

    为什么使用内嵌Tomcat开发? 开发人员无需搭建Tomcat的环境就可以使用内嵌式Tomcat进行开发,减少搭建J2EE容器环境的时间和开发时容器频繁启动所花时间,提高开发的效率. 怎么搭建内嵌To ...

  3. 内嵌tomcat最简单用法

    maven项目引入内嵌tomcat依赖 <dependency> <groupId>org.apache.tomcat.embed</groupId> <ar ...

  4. springboot去除内嵌tomcat和打包在tomcat中运行需要做的步骤

    去除内嵌tomcat和添加jsp依赖 去除内嵌tomcat 在springboot启动依赖中去除内嵌tomcat <dependency> <groupId>org.sprin ...

  5. 精尽Spring Boot源码分析 - 内嵌Tomcat容器的实现

    该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...

  6. 内嵌tomcat启动速度慢

    项目上最近要把内置的jetty换成tomcat, 来更好的支持servlet 3.0 本来以为换个容器, 几十行代码就好了. 实际上换了tomcat后, 一开始启动tomcat, 非常的慢. jett ...

  7. 查看和指定SpringBoot内嵌Tomcat的版本

    查看当前使用的Tomcat版本号 Maven Repository中查看 比如我们需要查Spring Boot 2.1.4-RELEASE的内嵌Tomcat版本, 可以打开链接: https://mv ...

  8. spring boot 2 内嵌Tomcat Stopping service [Tomcat]

    我在使用springboot时,当代码有问题时,发现控制台打印下面信息: Connected to the target VM, address: '127.0.0.1:42091', transpo ...

  9. 如何优雅的关闭基于Spring Boot 内嵌 Tomcat 的 Web 应用

    背景 最近在搞云化项目的启动脚本,觉得以往kill方式关闭服务项目太粗暴了,这种kill关闭应用的方式会让当前应用将所有处理中的请求丢弃,响应失败.这种形式的响应失败在处理重要业务逻辑中是要极力避免的 ...

随机推荐

  1. 7.2 Go type assertion

    7.2 Go type assertion 类型断言是使用在接口值上的操作. 语法x.(T)被称为类型断言,x代表接口的类型,T代表一个类型检查. 类型断言检查它操作对象的动态类型是否和断言类型匹配. ...

  2. scroll 方法 获取滚轴距离顶部高度

    $(window).scroll(function(){ var supportPageOffset = window.pageXOffset !== undefined; // 判断是否支持page ...

  3. 关于Slow HTTP Denial of Service Attack slowhttptest的几种慢攻击DOS原理

    关于Slow HTTP Denial of Service Attack  slowhttptest的几种慢攻击DOS原理 http://www.myhack58.com/Article/60/sor ...

  4. 405 - 不允许用于访问此页的 HTTP 谓词的处理办法

    今天介绍的是针对访问html页面时出现此类错误的处理办法,如果你的问题页面是其他类型,可以参考如下信息: IIS 返回 405 - 不允许用于访问此页的 HTTP 谓词.终极解决办法!!!! 1.为什 ...

  5. Mysql数值类型,小数点后保留两个零

    如有不足请帮忙留言区补充谢谢~ 一,数值类型保留小数点后两个0 在存入数据时,应客户需求数值类型,比如钱数,分数等等需要精确到小数点后几位. 800存入时显示为800.00 方法:在建表时直接定义此数 ...

  6. Verilog语言中的系统任务和系统函数

    Verilog语言中预先定义了一些任务和函数,用于完成一些特殊的功能,它们被称为系统任务和系统函数,这些函数大多数都是只能在Testbench仿真中使用的,使我们更方便的进行验证. `timescal ...

  7. vue-cli3或者4中如何正确的使用public中的图片

    标题说的很清楚了,就是要使用public中的图片 那么为什么要把图片放到public中呢,其实官网上面也说了,要么是需要动态引入非常多的图片,特别是小图标,如果放在assert中的话,会被webpac ...

  8. Istio 流量劫持过程

    开篇 Istio 流量劫持的文章其实目前可以在servicemesher社区找到一篇非常详细的文章,可查阅:Istio 中的 Sidecar 注入及透明流量劫持过程详解.特别是博主整理的那张" ...

  9. cypher语句摘要

    match(n) return n 返回所有的节点和关系,只要有就返回,对节点和关系的查找不做条件限制. match(n:Student) return n 返回所有的Student节点 创建节点:c ...

  10. ftp上传html文件

    在用ftp上传当个html文件时,发现html文件会被压缩成一行,在html中的单行注释将后面的代码都注释掉了,导致网页不能正常访问. 8uftp.FlashFXP.filezilla 在这三个ftp ...