前面所述的https://www.cnblogs.com/toov5/p/9823728.html 中的第一条先不赘述了,就是玩了maven
重点介绍后两条
首先内置Tomcat:
SpringBoot中内置tomcat服务器
Java操作创建tomcat
我们点击pom中的
持续点击到
继续点击会发现:
这个依赖 为我所需~
注意:Springboot2.0之后 Tomcat8.0以上版本
引入到自己的pom中(类似的还有其他的依赖包):
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.toov5.tomcat</groupId> <artifactId>tomcat</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!--Java语言操作tomcat --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>8.5.16</version> </dependency> <!-- tomcat对jsp支持 --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jasper</artifactId> <version>8.5.16</version> </dependency> </dependencies> </project>
servlet代码:
package com.toov5.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class IndexServlet extends HttpServlet { @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("successful congratulation "); } }
然后用servlet创建Tomcat容器
package com.toov5.tomcat; import org.apache.catalina.LifecycleException; import org.apache.catalina.core.StandardContext; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.Tomcat.FixContextListener; import com.toov5.servlet.IndexServlet; //使用Java创建Tomcat容器 //tomcat 底层执行程序 最终执行servlet //Springmvc底层使用servlet public class Test { private static int PORT = 8080; //端口 private static String CONTEX_PATH = "/toov5"; //项目访问名称 private static String SERVLET_NAME="IndexServet"; public static void main(String[] args) { Tomcat tomcatServer = new Tomcat(); tomcatServer.setPort(PORT); //指定端口号 //是否设置自动部署 tomcatServer.getHost().setAutoDeploy(false); //创建上下文,拿到上下文后就可以设置整个访问地址了 StandardContext standardContext = new StandardContext(); standardContext.setPath(CONTEX_PATH); //监听上下文 standardContext.addLifecycleListener(new FixContextListener()); standardContext.addLifecycleListener(new FixContextListener()); // tomcat容器添加standardContext 添加整个context tomcatServer.getHost().addChild(standardContext); // 创建servlet servlet的名字叫IndexServlet tomcatServer.addServlet(CONTEX_PATH, SERVLET_NAME, new IndexServlet()); // 添加servleturl映射 standardContext.addServletMappingDecoded("/index", SERVLET_NAME); try { tomcatServer.start(); } catch (LifecycleException e) { e.printStackTrace(); } System.out.println("tomcat服务器启动成功!"); //需要异步接受请求 tomcatServer.getServer().await(); } }
这段代码等同于,
web.xml中的:
<servlet> <servlet-name>toov5</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>toov5</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
项目目录结构:
启动:
访问路径名字是这个:
上下文:
访问之:
小伙伴,有木有很好玩呀~
原文地址:https://www.cnblogs.com/toov5/p/9823952.html
时间: 2024-11-02 20:30:04