[I am learning technology at Lagou training camp] Shocked! I completed the Tomcat source environment setup in three steps

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~~~

image-20200627101159036

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:

image-20200627103103808

We create a resource directory and put the conf directory and webapps directory under the resource directory.

image-20200627103354924

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.

image-20200627103606260

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.

image-20200627104257832

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.

image-20200627104720244

You can see that the startup has been successful, let's look up and down in our browser. enter:

localhost:8080

image-20200627104828079

What the hell? Report an error? Overturned? nonexistent.

image-20200627104930351

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);

image-20200627105205759

Then let's try again.

image-20200627105413285

perfect! ! !

image-20200627105517675

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.

Guess you like

Origin blog.csdn.net/qq_27790011/article/details/107017777