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

转自:https://www.jianshu.com/p/6bff04337753 

推荐这些文章:

Java:常用pom依赖集合

1. Tomcat依赖和JDK1.8版本注意事项

这些依赖同时都要放在<build><plugins>这里放插件依赖...</plugins></build>这两对标签中

<build>
<plugins>
<!-- ①添加Tomcat依赖 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
...

Java实现阿里云oss文件上传和下载

第一步:引入maven依赖

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!--阿里云oss-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<ver...

【Java/Dependency】ObjectMapper的依赖

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>

END

...

【Java/dependency】RestTemplate使用的依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>

END

...

动态代理报错:No default constructor found; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

缺少依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>...

2022.4.17 记录---Springboot-Maven个人通用pom文件

查看代码

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4....

SSM开发环境通用依赖(可直接拷贝)

<dependencies>
<!-- MySQL数据库连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>

<...

JAVA 编写测试类出现的问题(三)java.lang.NoClassDefFoundError: org/springframework/core/annotation/MergedAnnotations

java.lang.NoClassDefFoundError: org/springframework/core/annotation/MergedAnnotations
出现这个问题,找到此路径下的这个包发现都没有这个类。
实际上是我们引入的包的版本不对导致的。以上是我采用的pom包,当然有一些冗余,甄别自己的需要的去引用

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
&l...

配置信息记录

SpringMVC中:
pom.xml附加依赖参数:

<packaging>war</packaging>

<dependencies>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<ve...

解决:Plugin 'org.apache.maven.plugins:maven-resources-plugin:' not found

加入下面的配置就可以了

<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</dependency>

 

...

文章标题:Embed Tomcat Java(内嵌tomcat启动简述)
文章链接:https://www.dianjilingqu.com/51074.html
本文章来源于网络,版权归原作者所有,如果本站文章侵犯了您的权益,请联系我们删除,联系邮箱:saisai#email.cn,感谢支持理解。
THE END
< <上一篇
下一篇>>