Embed Tomcat Java(内嵌tomcat启动简述)
简单记录一下内部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启动简述)的更多相关文章
- Spring Boot移除内嵌Tomcat,使用非web方式启动
前言:当我们使用Spring Boot编写了一个批处理应用程序,该程序只是用于后台跑批数据,此时不需要内嵌的tomcat,简化启动方式使用非web方式启动项目,步骤如下: 1.在pom.xml文件中去 ...
- 基于内嵌Tomcat的应用开发
为什么使用内嵌Tomcat开发? 开发人员无需搭建Tomcat的环境就可以使用内嵌式Tomcat进行开发,减少搭建J2EE容器环境的时间和开发时容器频繁启动所花时间,提高开发的效率. 怎么搭建内嵌To ...
- 内嵌tomcat最简单用法
maven项目引入内嵌tomcat依赖 <dependency> <groupId>org.apache.tomcat.embed</groupId> <ar ...
- springboot去除内嵌tomcat和打包在tomcat中运行需要做的步骤
去除内嵌tomcat和添加jsp依赖 去除内嵌tomcat 在springboot启动依赖中去除内嵌tomcat <dependency> <groupId>org.sprin ...
- 精尽Spring Boot源码分析 - 内嵌Tomcat容器的实现
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- 内嵌tomcat启动速度慢
项目上最近要把内置的jetty换成tomcat, 来更好的支持servlet 3.0 本来以为换个容器, 几十行代码就好了. 实际上换了tomcat后, 一开始启动tomcat, 非常的慢. jett ...
- 查看和指定SpringBoot内嵌Tomcat的版本
查看当前使用的Tomcat版本号 Maven Repository中查看 比如我们需要查Spring Boot 2.1.4-RELEASE的内嵌Tomcat版本, 可以打开链接: https://mv ...
- spring boot 2 内嵌Tomcat Stopping service [Tomcat]
我在使用springboot时,当代码有问题时,发现控制台打印下面信息: Connected to the target VM, address: '127.0.0.1:42091', transpo ...
- 如何优雅的关闭基于Spring Boot 内嵌 Tomcat 的 Web 应用
背景 最近在搞云化项目的启动脚本,觉得以往kill方式关闭服务项目太粗暴了,这种kill关闭应用的方式会让当前应用将所有处理中的请求丢弃,响应失败.这种形式的响应失败在处理重要业务逻辑中是要极力避免的 ...
随机推荐
- pipeline的使用示例
搭建就不说了,直接示例如何使用pipeline. 一.以下输入参数:版本号为字符参数,按文档更新的是文本参数. 二.脚本对更新内容的处理如下: file_update_list="/home ...
- kali中安装漏洞靶场Vulhub(超详细)
前言 我们都知道,在学习网络安全的过程中,搭建漏洞靶场有着至关重要的作用.复现各种漏洞,能更好的理解漏洞产生的原因,提高自己的学习能力.下面我在kali中演示如何详细安装漏洞靶场Vulhub. 什么是 ...
- Gunicorn+Nginx+Flask项目部署
安装python3.6 1)前往用户根目录 >: cd ~ 2)下载 或 上传 Python3.6.7 >: wget https://www.python.org/ftp/python/ ...
- 记一次 React Native 大版本升级过程——从0.40到0.59
去年把公司几个react native 相关的项目升级了下,已经过去一段时间了,这里系统整理下之前的整个过程. 背景 之前到公司的时候发现公司用的还是0.40的版本,据了解,当时项目做的比较早,导航用 ...
- 用开源软件TrinityCore在Debian 10上搭建魔兽世界8.3.0.34220的服务器
用开源软件TrinityCore在Debian 10上搭建魔兽世界8.3.0.34220的服务器 TrinityCore是魔兽世界(World of Warcraft)的开源的服务端.目前支持魔兽的3 ...
- 加密通信软件Signal 2.92版本编译安装折腾手记(Ubuntu 18.04)
加密通信软件Signal 2.92版本编译安装折腾手记(Ubuntu 18.04) 前言 加密通信软件Signal是开源的,安全性很高,号称斯诺登也推荐大家使用.既然这么好,那必然会有不少人去尝试复制 ...
- mysql-connector-java 6版本的jdbc连接问题
使用新版本6的jdbc驱动,会出现下面的问题 Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The s ...
- [基础-001]C++字符串转换(char*,const char*,string)
1. string转const char* string str ="abc"; const char* charArr = str.c_str(); 2. const char* ...
- DataFrame的apply用法
DataFrame的apply方法: def cal_value_percent(row,total_value): row['new_column']=row[estimated_value_col ...
- Python基础知识思维导图
看不清的可以右键保存到本地,或者在新标签页中打开图片