Technology Sharing

Tomcat principle, structure, and design pattern

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1 what

A web server that runs Java servlet and JSP technology, can provide a running environment for Java web and process client requests through the HTTP protocol. That is, Tomcat = HTTP server + servlet container. Similar products include Jetty

  • Web Application:Web applications are applications accessed through a web browser, which use web technologies and standards (such as HTTP, HTML, CSS, JavaScript, etc.) to provide user interfaces and interactions with users. Web applications can include front-end and back-end components, with the front-end responsible for the user interface and presentation, and the back-end responsible for business logic and data processing.
  • Servlet: Servlet is part of the Java EE specification. It is a Java program running on the server side that receives requests from clients (such as web browsers), processes these requests (including accessing databases, calling business logic, etc.), and returns responses to the client. Servlet is one of the core technologies for building dynamic web content.

2 Structure

Connector + container

The container is the engine, host, context, servlet/wrapper

So the architecture is service (port 8080), connector, engine, host (http:localhost.com), context (/mvc, a web application), servlet/wrapper (/add, a servlet)

f090bd57045f4088948e1e7da63585ca.png

 

3 Key Components

3.1 connector

3.1.1 Endpoint

Used for: Network layer communication TCP/IP, such as NIONIO2

3.1.2 Processor

Used for: application layer protocol analysis, such as http

3.1.3 Adapter

Used for: unified conversion. Convert tomcat request/response to servlet request/response, and then pass it to the container.

  • Servlet request is a key interface in the Java Servlet API.
  • The Java Servlet API is a set of interfaces and classes used to develop server-side Java applications in Java EE (now known as Jakarta EE). Servlet is one of the three major components of Java Web (Servlet, Filter, Listener), which is mainly used to process client requests and generate responses.

3.2 Design Patterns

3.2.1 Design ideas for changing points and unchanged points

The invariant points are implemented with abstract classes and interfaces (such as ProtocolHanlder and AbstrctProtocol), and the variable points are implemented with specific classes (such as Http11NioProtocol and Http11Nio2Protocol).

47fd583736914b5e9911f0e38c739565.png

27e141a1618a4d7b8977cd9fc5a4f24a.png

f646124c6f57416c8648621ea75dbfa3.png

2357e1b0231e4de2b32e8c70f79b00b6.png

6fc9747fa2b14a649d16a3e70e00650d.png

d81414cef6f5481a87660f58599c0e96.png

3.2.2 Adapter Pattern

Adapter,tomcat用CoyoteAdapter。


3.2 container

3.2.1 How to manage engine, host, context, and servlet, using the combined mode to make them all implement Container

  • The Composite Pattern is a structural design pattern that allows you to combine objects into a tree structure to represent a "part-whole" hierarchy. In this pattern, the client's use of single objects and composite objects is consistent, that is, the client can handle single objects and their combinations in the same way.

To put it simply, a class/interface is used to assemble a tree structure (with a parent-child relationship). This is called combination. Then, because they all implement the same interface, the calls to each part are the same.

d785e0ea64ad4fb6952f9460fcbef154.png

3.2.2 engine, host, context, and servlet are called sequentially, using the responsibility chain mode Pipeline-Valve

Pipeline is a chain of responsibility, a queue, and the elements in the queue are valves. The valve can execute the processing logic and transfer to the next value.

The difference between valve and filter: valve is for Tomcat, filter is for Java Servlet API. Valve works at the web container level, intercepting all application requests, filter works at the application level, intercepting a web application request

08b1200a4585496e98a08c964952573a.png

3635bb9b0a7c46a1997621631c00212c.png

acb7a18568bd41c1b4ded295aeee2060.png

d28296d8ebba41d5acc6c209df8c0538.png

3.2.3 Engine management life cycle and allocation request

Assign request: map the mapper component. Find the corresponding wrapper layer by layer according to the URL.

Lifecycle: responsible for container creation, initialization, startup, destruction, etc. Each Container inherits LifeCycle. So it is also a composite mode.

The observer mode is also used (listening to change the life cycle status),

Combination mode (tree structure with parent-child relationship),

Template design pattern (write a general template (general logic), call new methods in the template, and implement the new methods in detail)

 

4 Architecture Design

  1. List requirements
  2. Object-oriented design class
  3. Find the changing and unchanging points in the requirements
  4. Use abstract classes for changing points and concrete classes for unchanged points