[Tomcat] Chapter 9: Handwritten embedded Tomcat plug-in (super simple)

1. Two types of tomcat

  • Traditional tomcat: requires jar deployment, started via BootStrap
  • Embedded tomcat: maven introduces the Tomcat class, custom startup plug-ins (Connector, Engin, Host, Context, Wrapper)
    • The root cause of embedding: open source
    • For example, SpringBoor uses embedded Tomcat
    • There is a default TomcatRunner plugin in the Tomcat class, which can be started; but you can still customize the tomcat plugin to do custom startup

2. Handwritten traditional tomcat

For details, please refer to: [Tomcat] Chapter 8: Handwriting Tomcat based on BIO 150 code (super detailed comment)

3. Handwritten embedded tomcat plugin

First post a screenshot of the project structure below:
Insert picture description here

3.1 Introduce dependencies in pom.xml

<dependencies>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
		
        <!-- 引入Tomcat类 -->
        <dependency>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
        </dependency>
</dependencies>

3.2 Prepare Servlet example

First write a Servlet example that handles business logic, which is used in the following plug-in configuration

public class DemoServlet extends HttpServlet {
    
    

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
    
    
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        response.getWriter().println("hello world!!!");

//        String message = request.getParameter("message");
//        response.getWriter().print(message);
    }
}

3.3 Writing plug-ins: EmbeddedTomcat (core)

Let me emphasize here that the purpose of the plug-in is to use existing resources flexibly. It can be as large as a compressed package of several tens of megabytes or as small as only one class. For example, the embedded tomcat plugin here, it only uses the following class EmbeddedTomcat to achieve flexible management and control of the resources related to the imported Tomcat package.

EmbeddedTomcat has two main functions:

  1. Achieve custom configuration and startup. That is, the related configuration of server.xml and web.xml of traditional tomcat are here.
  2. It is the entrance to start Tocmat.
public class EmbeddedTomcatServer {
    
    

    public static void main(String[] args) throws LifecycleException {
    
    

        Tomcat tomcat = new Tomcat();
        // 1.设置Connctor,包括端口
        Connector connector = tomcat.getConnector();
        connector.setPort(8080);
	    
        // 2.设置Engine,此处没什么可以设的...
        
        // 3.设置Host,包括域名,context路径 ---> 至此Tomcat可以正常启动,只不过没有
        Host host = tomcat.getHost();
        host.setAppBase("webapp");
        host.setName("localhost");
	    
        // 4.设置Context,包括host与应用的位置
        String classpath = System.getProperty("user.dir"); // C:\Users\13275\Documents\Java\...
        Context context = tomcat.addContext(host, "/", classpath);
        
        if (context instanceof StandardContext) {
    
    
            // Context还可以设置默认web.xml
            // StandardContext ctx = (StandardContext)context;
            // ctx.setDefaultContextXml("C:\\Users\\...\\web.xml");
            
            // 5.设置Wrapper(Servlet),及其映射路径(注:servlet中的@webServlet没用)
            Wrapper servlet = tomcat.addServlet("/", "DemoServlet", new DemoServlet());
            servlet.addMapping("/demo");
        }
		
        // 启动tomcat
        tomcat.start();
        // 防止main提前结束,使main阻塞
        tomcat.getServer().await();
    }
}

Demonstration of results

Start the main function of EmbeddedTomcat, and enter localhost:8080/demo in the browser, you can see the correct output.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43935927/article/details/108743198