introduction
Source of article content output: Lagou Education Java High Salary Training Camp;
With the popularity of microservices, lightweight server applications are also catching up. After all, those enterprise-level server applications are too expensive to deploy and build. And springboot comes with tomcat service.
So we also need to learn tomcat related knowledge, we also use a lot in the project, but we often just use it without in-depth understanding, but this is not enough for us.
So here are three steps to teach you to build a tomcat source code operating environment, so that everyone can open the door to understand tomcat in depth and move towards the pinnacle of life~~~
Download source code
To build the source code, of course you need to download the source code. I use 8.5.50 here. You can download it from the official website. You can also reply to "tomcat source code" in the background of the official account to get it.
After downloading, unzip as follows:
We create a resource directory and put the conf directory and webapps directory under the resource directory.
Create pom.xml
Create a pom.xml file in the decompressed root directory. After all, the bottom layer of tomcat is implemented by java. It is a maven project, I have created a pom.xml.
The content is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.tomcat</groupId>
<artifactId>apache-tomcat-8.5.50-src</artifactId>
<name>Tomcat8.5</name>
<version>8.5</version>
<build>
<!--指定源⽬录-->
<finalName>Tomcat8.5</finalName>
<sourceDirectory>java</sourceDirectory>
<resources>
<resource>
<directory>java</directory>
</resource>
</resources>
<plugins>
<!--引⼊编译插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<!--tomcat 依赖的基础包-->
<dependencies>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>javax.xml.soap</groupId>
<artifactId>javax.xml.soap-api</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
</project>
Import IDEA
Next, we directly import the tomcat project in idea. After the import is complete, some configuration needs to be done. We choose Bootstrap as the startup class.
And configure the VM parameters for the startup class Bootstrap, because the tomcat source code operation also needs to load configuration files and so on.
-Dcatalina.home=E:/IdeaProjects/apache-tomcat-8.5.50-src/resource
-Dcatalina.base=E:/IdeaProjects/apache-tomcat-8.5.50-src/resource
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=E:/IdeaProjects/apache-tomcat-8.5.50-src/resource/conf/logging.properties
Replace the absolute path here with the location of your computer.
test
After the above three steps, the operating environment of tomcat is actually set up. Let's click to run it.
You can see that the startup has been successful, let's look up and down in our browser. enter:
localhost:8080
What the hell? Report an error? Overturned? nonexistent.
This is because we did not initialize the JSP parsing engine, so we need to initialize it manually. How to do it?
Find the configureStart method of ContextConfig.
After webConfig(); add:
context.addServletContainerInitializer(new JasperInitializer(),null);
Then let's try again.
perfect! ! !
to sum up
Thinking that it is difficult, in fact, it's hard to discover when doing it. So everyone has to be brave enough to put it into practice~~~
I learned the second stage in Lagou training camp, and I started to learn more about tomcat and nginx. It turns out that tomcat was written in java, and the bottom layer is realized through socket.