简单记录一下内部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. linux常用命令---文件软硬链接

    文件链接

  2. Sql Server数据库导入Excel、txt数据详解,新人必看

    转自个人原创 https://blog.csdn.net/qq_15170495/article/details/104591606 数据库的要想导入数据,列的映射很是关键,只有列名匹配好,系统才知道 ...

  3. BZOJ1066 网络流

    拆点,将一个柱子拆成入点和出点,入点出点之间的容量就是柱子的容量    1066: [SCOI2007]蜥蜴 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多 ...

  4. J2EE项目中,servlet跳转到相应的JSP页面后,JSP页面丢失了样式效果

    原因: js和css的引用路径是相对路径.跳转后路径改变. 解决方法: 先在head标签中加入一下代码 <% String path = request.getContextPath(); St ...

  5. {dede:channelartlist} 改变偶数的class

    {dede:channelartlist} <div {dede:global.itemindex runphp='yes'} if((@me %2) == 0){ @me = 'class=& ...

  6. Flexible 应用

    Flexibl.js 为我们做了一项工作,媒体查询工作,节约了许多操作 举个例子,移动端的页面设计稿是750px,我们自己换算rem单位,比如我想把屏幕划分为15等份,我就750/15=50,然后用所 ...

  7. windows10安全及性能优化

    一.关闭一些服务. Google 更新服务 (gupdate) Google 更新服务 (gupdatem) HomeGroupListener HomeGroupProvider Xbox Live ...

  8. 新来的老大,剑走偏锋,干掉AOP做操作日志,实现后我们都惊呆了

    前言 用户在操作我们系统的过程中,针对一些重要的业务数据进行增删改查的时候,我们希望记录一下用户的操作行为,以便发生问题时能及时的找到依据,这种日志就是业务系统的操作日志. 本篇我们来探讨下常见操作日 ...

  9. 强力解决使用node版本管理工具 NVM 出现的问题(找不到 node,或者找不到 npm)

    nvm是好用的Nodejs版本管理工具, 通过它可以方便地在本地调换Node版本. 2020-05-28 当前长期稳定版12.17.0,于是 nvm install 12.17.0 然后C:/nvm/ ...

  10. for循环结构的使用

    /* for循环格式: for(①初始化条件; ②循环条件 :③迭代部分){ //④循环体 } 执行顺序:①-②-④-③---②-④-③-----直至循环条件不满足 退出当前循环 * */ publi ...