“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Tomcat is introduced

A brief introduction to a server. The concept of a server is very broad. It can be said that a server is a special computer, or it can be said that it refers to the application used to deploy a website. The server in JavaWeb is actually a web server, its essence is a software, the role is to publish our application, with users can access our application through the browser.

Common application servers are as follows:

Server name instructions
weblogic Implemented javaEE specification, heavyweight server, also known as javaEE container
websphereAS Implemented javaEE specification, heavyweight server.
JBOSSAS Implemented JavaEE specification, heavyweight server. Free of charge.
Tomcat JSP /servlet specification, is a lightweight server, open source free.

Tomcat download and installation

The official website is tomcat.apache.org/, and when opened it looks like this:

We can choose the version to download from the second election card on the left, here I downloaded 9, and then directly unzip to our designated address.

Directory after decompression:

Basic Use of Tomcat

1) Start and stop

Bat/startup.sh corresponds to the Windows running program and Linux running program respectively, and shutdown.bat/shutdown.sh corresponds to the shutdown.

Note that we must have configured the Java_Home environment variable before starting.

Double-click startup.bat to display the following figure:

Then open your browser and type: localhost:8080 to see:

If not displayed, possible but not limited reasons:

  1. The Java environment variable is not configured
  2. Port 8080 is occupied

CMD CMD CMD CMD CMD CMD CMD CMD CMD

Go to the logging.properties file under conf, The Java. Util. Logging. ConsoleHandler. Encoding = utf-8. This line is set to Java util. Logging. ConsoleHandler. Encoding = GBK, restart the service.

2) IDEA integrates with Tomcat server

Click Edit Configuration:

Find the Tomcat Server in the template and click Local to go to the tomcat installation path before configuration:

If the configuration is successful, create a new JavaEE project and you will find this icon in the lower left corner:

3) Tomcat release application -JavaWeb application

Start by creating a JavaWeb application in idea:

In this step, select JavaEE version 7, select web application below, and select your own version.

JavaWeb application directory structure in detail

After the creation of the Web project, the difference between the Java project and the normal time is that there is a Web folder. In the Web folder, we can directly store our CSS or HTML and JSP files, while in the WEB-INF folder, we can store configuration files.

JavaWeb application deployment

The first step

The second step

The third step

Just start it.

Open to:

You can also change the project access path:

The contents of the display interface can be implemented by modifying index.jsp.

War file release

Step 1: Use the name of the jar-cvf war package which resources in the current directory to type into war****

Step 2: Copy the typed WAR to the Tomcat webapps directory

Step 3: Tomcat decompresses automatically when the service is started.

Configuring default Ports

The access port is configured in the main configuration file of the Tomcat server. The value written in the configuration file is 8080. But it is not the default port for Tomcat. The default port we are talking about here is the port number that can be omitted when using the default value. The default port of Tomcat is 80.

The configuration mode is as follows:

<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />       
Copy the code

The default Tomcat port for opening the browser is 8080, and the default HTTP port is 80. You must specify port 8080 or change the port number to 80 when accessing the Tomcat service.

The change path is server.xml in conf folder. Here is a detailed explanation of this file (from Dark Horse Java) :

<? The XML version = "1.0" encoding = "utf-8"? > <! Represents the entire Servlet container component and is the top-level element. Server port="8005" shutdown=" shutdown "> <! -- Listener for Tomcat to run --> <! -- Default lifecycle listener: Before to print the server is initialized and server operating system, JVM version information - > < Listener className = "org. Apache. Catalina. Startup. VersionLoggerListener" / > <! -- Default lifecycle listener: Load the APR library before server initialization, And destroyed after the server stop - > < Listener className = "org. Apache. Catalina. Core. AprLifecycleListener" SSLEngine = "on" / > <! -- Default lifecycle listener: Called before server initialization, Solve singletons created as a result of the jVM memory leak problem and lock file - > < Listener className = "org. Apache. Catalina. Core. JreMemoryLeakPreventionListener" / > <! -- Default lifecycle listener: Server startup JNDI resources registration for an administration - > < Listener className = "org. Apache. Catalina. Mbeans. GlobalResourcesLifecycleListener" / > <! -- Default lifecycle listener: Rebuild threads in the Executor pool when the context stops, To avoid memory leaks -- > < Listener className = "org. Apache. Catalina. Core. ThreadLocalLeakPreventionListener" / > <! -- Global resource allocation, Start creating the JNDI context when this node is resolved --> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <! The Tomcat service is configured with <Engine> elements and <Connector> elements. <Connector> can be one or more, and they share the configuration of the <Engine> --> <Service name="Catalina"> <! The default thread pool is StandardThreadExecutor. You can specify the level of Catalina shared thread pool as Service via the className attribute of the tag. By default, the shared thread pool is not configured. -- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/>--> <! Protocol: specifies the protocol name. ConnectionTimeout: specifies the connectionTimeout time. RedirectPort: specifies the connectionTimeout time. If resources must be accessed using HTTPS, Tomcat redirects access to this port. <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" AJP protocol = "/ 1.3" redirectPort = "8443" / > <! <Engine name="Catalina" defaultHost="localhost"> <! Configure Tomcat's Realm domain Safe space - > < Realm className = "org. Apache. Catalina. Realm. LockOutRealm" > < Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <! -- Configure the virtual host. AppBase: specifies the default release directory for the application. UnpackWARs: Specifies whether to automatically decompress the WAR package. AutoDeploy: specifies whether to automatically decompress the war package. -> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <! - log valve intercept - > < valve className = "org. Apache. Catalina. Valves. AccessLogValve" directory = "logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot; %r&quot; %s %b" /> <! - Configure specific application items. DocBase is the location of the project and can be an absolute or relative path. If it is a relative path, it must be published in the default application publishing directory of the virtual host. Path specifies the URI used to access the Web application. Reloadable specifies whether to reload /WEB-INF/classes and/web-INF /lib. --<Context docBase="tomcat" path="/tomcat" reloadable="true"/>--> </Host> </Engine> </Service> </Server>Copy the code

Tomcat configures a virtual directory

You can configure a virtual directory in two ways. The first is by adding labels to the master configuration file. The second is by writing a separate configuration file.

The first way is to add a
element to the

element of server.xml. Path: access the resource URI. The URI name can be arbitrary, but must be preceded by/docBase: the physical address of the disk where the resource resides. The second option is to write a separate XML file with a filename that you can choose. Write a
element inside the file. This file should be placed in conf\Catalina\localhost in the Tomcat directory. Note that when a separate configuration file is used, the access resource URI becomes the name of the /+ file. And the path property of the Context is invalidated.

Tomcat Configures virtual hosts

Add a
element to the

element, where: name: specifies the name of the Host appBase: specifies the application publishing directory of the current Host unparkWARs: AutoDeploy: Specifies whether the WAR package is automatically decompressed upon startup

The configuration example is as follows:

<Host name="www.itcast.cn" appBase="D:\itcastapps" unpackWARs="true" autoDeploy="true"/>
​
<Host name="www.itheima.com" appBase="D:\itheimaapps" unpackWARs="true" autoDeploy="true"/>
Copy the code