I teach you to write your own Tomcat container in Java. It’s up..

Time:2022-6-20

When we first came into contact with the springboot project, we were particularly surprised at why the web project could run without the Tomcat server. Why can a web project run with only one main method. After studying this article, you will understand.

1. discover the built-in Tomcat

Open a springboot project and we find that it depends on Tomcat

Next, let’s find the dependency of Tomcat

First find spring boot starter Web

After clicking in, we saw that he was using version 2.1.0. We continued to click

Sure enough, we found the dependence of Tomcat in it. Let’s continue to click on it

This is where we can see the relevant dependencies of Tomcat.

Therefore, we can use java to provide a built-in Tomcat container framework and use the Java language to operate the Tomcat container. In this way, we can also create a maven project with a built-in Tomcat container.

2. create a maven project

slightly

3. add Tomcat dependency

Here we use version 8.5 of Tomcat

org.apache.tomcat.embed
 tomcat-embed-core
 8.5.16

4. create a 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");
 }
}

Let’s first look at how traditional servlets need to be configured. We need the web In the XML file, configure the mapping of the servlet

When we access this servlet, the access address should be:ip://index

But, but, but Tomcat and web How to configure XML without XML? Simply, we use the main method to create a Tomcat.

5. create a Tomcat

5.1. Define servlet configuration parameters

Let’s first define several servlet configuration parameters: servlet name and servlet mapping

5.2. Introduction to common built-in Tomcat APIs

org.apache.catalina.startup.Tomcat

Create Tomcat

Tomcat tomcatServer = new Tomcat();

Set Tomcat port

void setPort(int port);

Set context address

void setPath(String path)

Add listening

void addLifecycleListener(LifecycleListener listener)

Add a servlet

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

Add a servlet map

void addServletMappingDecoded(String pattern, String name)

Start Tomcat

void start() throws LifecycleException

Access to services

Server getServer()

Service asynchrony

tomcatServer.getServer().await()

Add the web application to the webapps directory of tomcat, the equivalent default web XML will be applied to the web application, and anyWEB-INF/web.xmlandMETA-INF/context.xmlWill be handled normally.

Context addWebapp(String contextPath, String docBase) throws ServletException

Add Web application resources to this web application

void addPreResources(WebResourceSet webResourceSet)

5.3. Create Tomcat and add 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 ("start Tomcat");
  Tomcat tomcatServer = new Tomcat();
  //Set Tomcat port
  tomcatServer.setPort(PORT);
  //Whether this Tomcat port is automatically deployed
  tomcatServer.getHost().setAutoDeploy(false);
  //Create a web application
  StandardContext standardContex = new StandardContext();
  //Set the context address of the web application
  standardContex.setPath(CONTEX_PATH);
  //Add listening for web applications
  standardContex.addLifecycleListener(new FixContextListener());
  //Add a web application to the server
  tomcatServer.getHost().addChild(standardContex);
  //Configure servant and mapping
  tomcatServer.addServlet(CONTEX_PATH, SERVLET_NAME, new IndexServlet());
  standardContex.addServletMappingDecoded("/"+MAPPING, SERVLET_NAME);
  //Start Tomcat
  tomcatServer.start();
  System. out. Println ("start Tomcat completed");
  //Start asynchronous service to receive requests
  tomcatServer.getServer().await();
 }
}

5.4. Start the main method to create Tomcat

The startup results are as follows. We can see that Tomcat starts on port 8080.

And tomcat 8080 folder

5.5. Access indexservlet

Request address:ip://index

Request address:127.0.0.1:8080/demosevlet/index

Original link:https://blog.csdn.net/m0_37892044/article/details/107963187

Copyright notice: This article is the original article of CSDN blogger “waitao” and follows the CC 4.0 by-sa copyright agreement. Please attach the original source link and this notice for reprint.

Recent hot article recommendations:

1.1000+ Java interview questions and answers (2021 latest version)

2.Stop playing if/ else on the full screen. Try the strategy mode. It smells good!!

3.what the fuck! What new syntax is XX ≠ null in Java?

4.Spring boot 2.5 was released. The dark mode is too explosive!

5.Java development manual (Songshan version) is the latest release. Download it quickly!

Feel good, don’t forget to like + forward!