教你用Java的方式創(chuàng)建一個自己的Tomcat容器
當(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
- <!--Java語言操作tomcat -->
- <dependency>
- <groupId>org.apache.tomcat.embed</groupId>
- <artifactId>tomcat-embed-core</artifactId>
- <version>8.5.16</version>
- </dependency>
4.創(chuàng)建一個servlet:IndexServlet
- public class IndexServlet 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("IndexServletIndexServletIndexServletIndexServletIndexServletIndexServlet");
- }
- }
我們先看看傳統(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
- Tomcat tomcatServer = new Tomcat();
設(shè)置tomcat端口
- void setPort(int port);
設(shè)置上下文地址
- void setPath(String path)
添加監(jiān)聽
- void addLifecycleListener(LifecycleListener listener)
添加sevlet
- Wrapper addServlet(String contextPath,String servletName,Servlet servlet)
添加sevlet映射
- void addServletMappingDecoded(String pattern, String name)
啟動tomcat
- void start() throws LifecycleException
獲取服務(wù)
- Server getServer()
服務(wù)異步
- 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都將被正常處理。
- Context addWebapp(String contextPath, String docBase) throws ServletException
web應(yīng)用資源添加到此Web應(yīng)用程序
- void addPreResources(WebResourceSet webResourceSet)
5.3.創(chuàng)建tomcat并且添加servlet
- public class TestTomcat {
- private static int PORT = 8080;
- private static String CONTEX_PATH = "/demosevlet";
- private static String SERVLET_NAME = "index";
- private static String MAPPING = "index";
- public static void main(String[] args) throws LifecycleException, InterruptedException {
- System.out.println("開始啟動tomcat");
- Tomcat tomcatServer = new Tomcat();
- //設(shè)置tomcat端口
- tomcatServer.setPort(PORT);
- //此tomcat端口是否自動部署
- tomcatServer.getHost().setAutoDeploy(false);
- //創(chuàng)建一個web應(yīng)用程序
- StandardContext standardContex = new StandardContext();
- //設(shè)置web應(yīng)用程序的上下文地址
- standardContex.setPath(CONTEX_PATH);
- //添加web應(yīng)用程序的監(jiān)聽
- standardContex.addLifecycleListener(new FixContextListener());
- //將web應(yīng)用程序添加到服務(wù)器中
- tomcatServer.getHost().addChild(standardContex);
- //配置servelt和映射
- tomcatServer.addServlet(CONTEX_PATH, SERVLET_NAME, new IndexServlet());
- standardContex.addServletMappingDecoded("/"+MAPPING, SERVLET_NAME);
- //啟動tomcat
- tomcatServer.start();
- System.out.println("啟動tomcat完畢");
- //開啟異步服務(wù),接收請求
- tomcatServer.getServer().await();
- }
- }
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