自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教你用Java的方式創(chuàng)建一個自己的Tomcat容器

開發(fā) 后端
當(dāng)我們開始接觸到SpringBoot項目的時候,我們特別驚訝,為什么沒有了tomcat服務(wù)器,web項目還能跑起來。為什么只用了一個main方法就能講一個web項目跑起來。學(xué)習(xí)了這篇文章你就能明白了。

 當(dāng)我們開始接觸到SpringBoot項目的時候,我們特別驚訝,為什么沒有了tomcat服務(wù)器,web項目還能跑起來。為什么只用了一個main方法就能講一個web項目跑起來。學(xué)習(xí)了這篇文章你就能明白了。

1.發(fā)現(xiàn)內(nèi)置Tomcat

打開一個SpringBoot項目,我們發(fā)現(xiàn),有tomcat的依賴

接下來讓我們把tomcat的依賴找到

首先找到spring-boot-starter-web

點進去之后我們看到他用的是2.1.0版本,我們繼續(xù)點擊去

果然在里面找到tomcat的依賴,我們繼續(xù)點進去

這是我們我們就能看到tomcat的相關(guān)依賴了。

因此我們是可以使用Java提供內(nèi)置Tomcat容器框架,使用Java語言操作Tomcat容器。這樣我們也能創(chuàng)建一個maven項目,并且內(nèi)置一個tomcat容器了。

2.創(chuàng)建一個maven項目

3.添加tomcat依賴

這里我們使用8.5版本的tomcat 

  1. <!--Java語言操作tomcat -->  
  2. <dependency>  
  3.  <groupId>org.apache.tomcat.embed</groupId>  
  4.  <artifactId>tomcat-embed-core</artifactId>  
  5.  <version>8.5.16</version>  
  6. </dependency> 

4.創(chuàng)建一個servlet:IndexServlet 

  1. public class IndexServlet extends HttpServlet {  
  2.  private static final long serialVersionUID = 1L 
  3.  @Override  
  4.  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
  5.   doPost(req, resp);  
  6.  }  
  7.  @Override  
  8.  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
  9.   resp.getWriter().print("IndexServletIndexServletIndexServletIndexServletIndexServletIndexServlet");  
  10.  }  

我們先看看傳統(tǒng)的Servlet需要怎么配置,我們需要在WEB-INF目錄下的web.xml文件中,配置sevlet的映射

當(dāng)我們訪問這個servlet的時候,訪問地址應(yīng)該是:ip:<端口>/<tomcat上下文>/index

但是,但是,但是tomcat和web.xml沒了,怎么配置?簡單,沒有我們就用main方法來創(chuàng)建一個tomcat。

5.創(chuàng)建一個tomcat

5.1.定義servlet配置參數(shù)

我們先定義幾個servlet的配置參數(shù):servlet的名字,servlet的映射

5.2.內(nèi)置Tomcat常用API介紹

org.apache.catalina.startup.Tomcat

創(chuàng)建tomcat 

  1. Tomcat tomcatServer = new Tomcat(); 

設(shè)置tomcat端口 

  1. void setPort(int port); 

設(shè)置上下文地址 

  1. void setPath(String path) 

添加監(jiān)聽 

  1. void addLifecycleListener(LifecycleListener listener) 

添加sevlet 

  1. Wrapper addServlet(String contextPath,String servletName,Servlet servlet) 

添加sevlet映射 

  1. void addServletMappingDecoded(String pattern, String name) 

啟動tomcat 

  1. void start() throws LifecycleException 

獲取服務(wù) 

  1. Server getServer() 

服務(wù)異步 

  1. tomcatServer.getServer().await() 

將Web應(yīng)用程序添加到Tomcat的webapps目錄中,等效的默認(rèn)web.xml將應(yīng)用于該Web應(yīng)用程序,并且與該應(yīng)用程序打包的任何WEB-INF/web.xml和META-INF/context.xml都將被正常處理。 

  1. Context addWebapp(String contextPath, String docBase) throws ServletException 

web應(yīng)用資源添加到此Web應(yīng)用程序 

  1. void addPreResources(WebResourceSet webResourceSet) 

5.3.創(chuàng)建tomcat并且添加servlet 

  1. public class TestTomcat {  
  2.  private static int PORT = 8080 
  3.  private static String CONTEX_PATH = "/demosevlet" 
  4.  private static String SERVLET_NAME = "index" 
  5.  private static String MAPPING = "index" 
  6.  public static void main(String[] args) throws LifecycleException, InterruptedException {  
  7.   System.out.println("開始啟動tomcat");  
  8.   Tomcat tomcatServer = new Tomcat();  
  9.   //設(shè)置tomcat端口  
  10.   tomcatServer.setPort(PORT);  
  11.   //此tomcat端口是否自動部署  
  12.   tomcatServer.getHost().setAutoDeploy(false);  
  13.   //創(chuàng)建一個web應(yīng)用程序  
  14.   StandardContext standardContex = new StandardContext();  
  15.   //設(shè)置web應(yīng)用程序的上下文地址  
  16.   standardContex.setPath(CONTEX_PATH);  
  17.   //添加web應(yīng)用程序的監(jiān)聽  
  18.   standardContex.addLifecycleListener(new FixContextListener());  
  19.   //將web應(yīng)用程序添加到服務(wù)器中  
  20.   tomcatServer.getHost().addChild(standardContex);  
  21.   //配置servelt和映射 
  22.   tomcatServer.addServlet(CONTEX_PATH, SERVLET_NAME, new IndexServlet());  
  23.   standardContex.addServletMappingDecoded("/"+MAPPING, SERVLET_NAME); 
  24.    //啟動tomcat  
  25.   tomcatServer.start();  
  26.   System.out.println("啟動tomcat完畢");  
  27.   //開啟異步服務(wù),接收請求  
  28.   tomcatServer.getServer().await();  
  29.  }  

5.4.啟動main方法創(chuàng)建tomcat

啟動結(jié)果如下,我們看到tomcat以8080端口啟動。另外,關(guān)注Java知音公眾號,回復(fù)“后端面試”,送你一份面試題寶典!

并且在此目錄下生成了tomcat.8080文件夾

5.5.訪問IndexServlet

請求地址:ip:<端口>/<tomcat上下文>/index

請求地址:127.0.0.1:8080/demosevlet/index

 

 

責(zé)任編輯:龐桂玉 來源: Java知音
相關(guān)推薦

2015-04-22 11:29:45

PythonPython創(chuàng)建瀑布圖

2020-10-15 15:01:54

Python 開發(fā)編程語言

2020-11-20 10:50:01

Docker容器

2020-05-09 09:59:52

Python數(shù)據(jù)土星

2021-05-13 20:20:40

Java架構(gòu)代碼

2020-09-15 14:05:21

Python代碼預(yù)測模型

2022-06-28 12:35:21

DockerPython

2019-12-09 15:00:48

TomcatServlet容器

2019-08-26 19:03:58

2022-02-25 09:41:05

python搜索引擎

2021-08-24 10:02:21

JavaScript網(wǎng)頁搜索 前端

2021-07-12 09:03:50

Python任務(wù)管理器cmd命令

2022-01-06 18:20:20

Scarlet AndroidWebSocket

2024-11-05 16:40:24

JavaScript搜索引擎

2021-05-18 14:42:55

PythonMySQL

2019-01-24 09:00:00

PythonAutoML機器學(xué)習(xí)

2018-12-17 09:10:52

機器學(xué)習(xí)TensorFlow容器

2021-07-12 14:35:26

代碼架構(gòu)云原生

2021-04-25 08:58:00

Go拍照云盤

2018-12-10 10:00:06

Python神經(jīng)網(wǎng)絡(luò)編程語言
點贊
收藏

51CTO技術(shù)棧公眾號