Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of tomcat+spring mvc

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the principle of tomcat+spring mvc". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the principle of tomcat+spring mvc".

Tomat + spring mvc is a popular Java micro-service architecture, including the current spring boot and spring boot-based further application encapsulation of the spring cloud framework, the underlying framework is based on tomcat + spring mvc. Therefore, learning the basic principles of tomcat + spring mvc will be of great help to solve the problems in use.

The operation mode of tomcat + spring mvc

Before you understand how tomcat+spring mvc works, it's best to configure the tomcat environment yourself, then deploy a spring mvc service, get a feel for the whole process, and get a general idea of the exposed configurations of tomcat and spring mvc. In this way, with a perceptual understanding, and then in-depth understanding of the principle, there will be a better understanding. There are already a lot of detailed articles about tomcat environment building on the Internet, so I won't repeat them here.

Traditional services that use the tomcat + spring mvc framework need to configure the tomcat environment before development. This process includes downloading and installing tomcat, configuring environment variables, modifying tomcat's configuration file, and finally running tomcat. Yes, you read it right. Tomcat runs independently, while spring mvc's service is loaded by the tomcat runtime. Services based on the spring mvc framework can be created automatically in popular java IDE, such as eclipse or idea. The basic configuration of the automatically created project has been written, and you can write the business code directly, which is a significant advantage of this architecture. Of course, you can also create an empty project and configure it yourself. Overall, the configuration of spring mvc config files based on xml or java code is cumbersome. Finally, the project needs to be packaged into a war file, copied to the specified directory of tomcat, tomcat will automatically unpack and load, and then based on your configuration, visit the specified routing link of the specified port, you can request a reply from the server.

Obviously, this mode of operation requires: tomcat needs to monitor the specified directory, and once a new war package is added, it completes the ability to unpack and dynamically load the compiled class; tomcat needs a mechanism for opening and closing network ports and monitoring, and maintains a thread pool to handle requests that may come at any time; tomcat also needs to smoothly pass the incoming request to the spring mvc service, and then send the data returned by the service.

Basic container composition within tomcat

The so-called Container does not refer to the classes used to store data structures, such as Collection, Set, List, etc., but refers to the concepts (classes or interfaces) used in tomcat and spring mvc to abstract functions and structures hierarchically. The basic container structure of tomcat is shown in the figure:

The top-most container of tomcat, Server, represents the entire tomcat server, and a Server can contain one or more Service. A Service contains one or more Connetor, but only one Container. Connector is mainly used to deal with connection-related matters, such as listening of network socket Socket, receiving of request request and sending of reply response. Container here is an abstract concept, an interface. It means that a Service can contain only one Engine--Engine inherited from Container. The Engine container structure is shown in the figure:

Where Engine, Host, Context, and Wrapper all inherit from Container.

Engine is used to manage multiple sites, that is, there can be multiple Host. Host represents a site, which is actually a virtual host. Context, context, represents a set of applications, a set of applications always have the same application context configuration. Finally, there is Wrapper, where there is only one Servlet per wrapper, and each Servlet corresponds to each developed spring mvc service program. So all the spring mvc service programs we developed are loaded into tomcat through Wrapper. Context and Wrapper are added dynamically, and each time we add a war package under the specified directory of tomcat, when tomcat loads the war package, we can add Context and Servlet.

External configuration corresponding to tomcat container

Before emphasizing the principles of learning, it is best to configure the tomcat environment and deploy a spring mvc service program yourself, because many of the configurations of tomcat are related to internal containers. The combination of external perceptual understanding and rational understanding of internal principles is beneficial to the deepening of cognition and memory.

Under the tomcat installation directory (the default directory for linux yum installation is / usr/local/tomcat, and Baidu is required for other systems and installation methods), there is a conf folder that stores some tomcat-level container configurations. Server.xml configures Service, Connector, Engine and Host under the Server container in a format similar to the nested structure shown in the figure above, but in the form of xml. Take one of the Connector configurations as an example:

As mentioned above, Connector is mainly used to deal with connection-related things, such as listening of network socket Socket, receiving of request request and sending of reply response. So the port, network protocol, connection timeout, and redirect port are configured here. The configuration of other containers is also similar, so you can open it and browse it. The context.xml file in the conf directory stores the configuration of the Context container:

WEB-INF/web.xml ${catalina.base} / conf/web.xml

It mainly includes the directory configuration of two web.xml. According to the container structure mentioned above, a Context container can have one or more Wrapper, and each Wrapper contains a Servlet,Servlet, which is the spring mvc service program we developed. What is written in web.xml is the relevant configuration of spring mvc Servlet. In a general spring mvc project, there will be a WEB-INF directory containing a self-configured web.xml file. The other is tomcat's default web.xml configuration, which you can see is web.xml in the conf directory. This default configuration is for all spring mvc programs deployed in tomcat. The specific explanation of the content of web.xml configuration is put in the overview of spring mvc principles later.

Thank you for your reading, the above is the content of "what is the principle of tomcat+spring mvc", after the study of this article, I believe you have a deeper understanding of what the principle of tomcat+spring mvc is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report