[Tomcat] Chapter 1: Tomcat Directory Structure & Configuration File Analysis & Three Application Deployment Methods

So far, Tomcat has become a mature Servlet container product, and serves as an embedded Servlet container for servers such as JBoss

Standard JDK version 6.X 7.X 8.X 8.5.X 9.X
JDK 》=5.0 》=6.0 》=7.0 》=7.0 》=8.0
Servlet 2.5 3.0 3.1 3.1 4.0
JSP 2.1 2.2 2.3 2.3 2.3
HE 2.1 2.2 3.0 3.0 3.0
WebSocket N/A 1.1 1.1 1.1 1.1

After opening the decompressed directory of tomcat, you can see the following directory structure:
ç ›®å½ • ç» “æž„
Let's analyze the contents of these directories one by one:

1.bin

The contents of the bin directory are shown in the following figure: Mainly used to store Tomcat commands, and many environment variables are also set here.

Note: You need to configure the JAVA_HOME environment variable before starting

  • The commands ending in .sh represent Linux commands
    • startup.bat represents the command to start Tomcat under windows system;
    • shutdown.bat represents the command to shut down Tomcat under Windows system.
  • Ending with .bat represents the command under Windows
    • startup.sh represents the command to start Tomcat under linux system;
    • shutdown.sh represents the command to shut down Tomcat under linux.

binç ›®å½ •

2.conf

confç ›®å½ •

2.1 catalina.policy

Permission related to Permission, Tomcat runs on jvm, so there are some default permissions.

2.2 server.xml

Configure the entire server information. For example, modify the port number, add a virtual host, etc.;

<Server>
    <Service>(n个)
        <Connector />(n个)
        <Engine>(1个)
               <host>(n个)
                   <Context />(n个)
               </host>
        </Engine>
    </Service>
</Server>
  • <Server>element

    It represents the entire container and is the top element of the Tomcat instance. It is defined by the org.apache.catalina.Server interface and contains one or more elements.

     <Server port="8005" shutdown="SHUTDOWN">
    
    • port : Specify the port for Tomcat to listen to the shutdown command. When the server is terminated, the shutdown command must be issued on the machine where the Tomcat server is located. This property must be set.
    • shutdown : Specifies the string of the shutdown listening port sent to the Tomcat server when the Tomcat server is terminated. This attribute must be set.
  • <Service>element

    Contains one element, and one or more elements , these elements share the same element .

  • <Connector>element

    Receive the client connection request, create Request and Response objects for exchanging data with the requester, and then allocate threads to let the Engine handle the request, and pass the generated Request and Response objects to the Engine. By configuring the Connector, you can control the service request Protocol and port number.

    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
    
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    
    • port : the port number monitored by the server, that is, the port number requested by the client;

    • protocol : specifies the request protocol;

    • redirectPort: indicates that when the request is https, redirect to the Connector with port number 8443;

    • connectionTimeout: indicates the timeout period of the connection, in milliseconds;

    • minSpareThreads : Indicates the minimum number of threads that the Connector waits for client requests. Each request is responsible for one thread, and the default is 10;

      ​ If the initial value is set large, the thread creation time can be reduced when the request comes

    • maxThreads : Represents the maximum number of request processing threads to be created by this connector, that is, the maximum number of concurrent requests that can be processed, the default is 200;

      ​ Generally determined by the performance of the machine

    • maxConnections: Represents the maximum number of connections accepted and processed by the server at any given time. When this number is reached, the client request will be placed in the request queue, the default maximum number of queues is the acceptCount parameter value, the default is maxThreads in BIO mode, and the default is 10000 in NIO mode;

    • acceptCount: When maxConnections reaches the maximum value, that is, when all request threads are in use, the maximum queue length of incoming connection requests. Any request received when the queue is full will be rejected. The default value is 100.

    • maxHeaderCount: The maximum number of headers allowed in the request. Requests that contain more headers than the specified limit will be rejected. Less than 0 means unlimited, and the default value is 100.

  • <Engine>element

    There is one and only one Engine component in the Service component . The Engine is the request processing component in the Service component. The Engine component receives requests from one or more Connectors and processes them, and returns the completed response to the Connector, which is finally delivered to the client.

    <Engine name="Catalina" defaultHost="localhost">
    
    • name : The attribute is used for log and error messages and should be unique in the entire server. Generally the default engine is Catalina (bomber)

    • defaultHost: The attribute specifies the default host name. When the host name specified in the request to this machine does not exist, the host specified by defaultHost will be used for processing. Therefore, the value of defaultHost must be the same as the name attribute of a Host component in the Engine Value matches.

  • <Host>element

    The Engine element contains at least one or more Host elements . Each Host element defines a virtual host, which can contain one or more Web applications. The name of one of the Hosts must match the defaultHost attribute of the Engine component.

    <Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true">
    
    • name: The name of the virtual host.
    • appBase : Specify the directory of the virtual host. You can specify an absolute directory or a relative directory relative to <CATALINA_HOME>. If this item is not set, the default value is <CATALINA_HOME>/webapps.
    • unpackWARs: If this item is set to true, it means that the war file of the web application will be decompressed into an open directory structure before running. If it is set to false, the war file will be run directly.
    • autoDeploy: If this item is set to true, it means that when the Tomcat server is running, the files under appBase can be monitored. If a new web application is added, the web application will be automatically released.

Note: In addition, in the element may comprise the following sub-elements: <Logger>,<Realm>,<Valve>,<Context>.

  • <Context>element

    Each Context element represents a single web application running on a virtual host, and one element can contain multiple elements.

    <Context path="/sample" docBase="sample" debug="0" reloadable="true"> 
    
    • path : Specify the URL entry to access the Web application.

    • docBase : Specify the file path of the web application. It can be an absolute path or a relative path relative to the host's appBase attribute. If the web application adopts an open directory structure, then specify the root directory of the web application; if the web application is a WAR file , Then specify the path of the WAR file.

    • reloadable : Hot deployment. If this attribute is set to true, the Tomcat server will monitor the changes of the CLASS files in the WEB-INF/class and WEB-INF/lib directories when it is running. If it detects that the calss file has been updated, the server will Automatically reload the web application.

2.3 web.xml

  • Tomcat will load its own web.xml first, and then load the web.xml dedicated to each javaweb project (not required)

  • MIME: File type is actually the file type handled by Tomcat.

    These MIME types are used to indicate the document type between the client and the server. If a user requests an html web page, the server will also tell the client that the browser responds to a document of text/html type, which is a MIME type. The client browser knows how to handle it through this MIME type. Of course, the html file is displayed in the browser. But if the server responds to an exe file, then the browser cannot display it, but a download window should pop up. MIME is used to describe what type of content the document is!

<web-app>    
<display-name></display-name>定义了WEB应用的名字    

<description></description> 声明WEB应用的描述信息    

<context-param></context-param> context-param元素声明应用范围内的初始化参数。    

<filter></filter> 过滤器元素将一个名字与一个实现javax.servlet.Filter接口的类相关联。    
<filter-mapping></filter-mapping> 一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个
    							servlet或JSP页面相关联。 

<listener></listener>servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话
    				或servlet环境时得到通知.Listener元素指出事件监听程序类。    

--------------------------------------------------------------------------------------------------
<servlet></servlet> 在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。   
<servlet-mapping></servlet-mapping> Servlet对应的URL如(http://host/webAppPrefix/servletName)
    							      
<mime-mapping></mime-mapping>如果Web应用具有想到特殊的文件,希望能保证给他们分配特定的MIME类型,则mime-
    						mapping元素提供这种保证。
--------------------------------------------------------------------------------------------------
<session-config></session-config> 如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存。  

<welcome-file-list></welcome-file-list> 服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件。  

<error-page></error-page> 在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面。
    
<taglib></taglib> 对标记库描述符文件(Tag Libraryu Descriptor file)指定别名。此功能使你能够更改TLD文件
    			的位置,而不用编辑使用这些文件的JSP页面。  

<resource-env-ref></resource-env-ref>声明与资源相关的一个管理对象。 

<resource-ref></resource-ref> 声明一个资源工厂使用的外部资源。

<security-constraint></security-constraint> 制定应该保护的URL。它与login-config元素联合使用   

<login-config></login-config> 指定服务器应该怎样给试图访问受保护页面的用户授权。它与sercurity-
    						constraint元素联合使用。  

<security-role></security-role>给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-
    					ref元素的role-name子元素中。分别地声明角色可使高级IDE处理安全信息更为容易。  

<env-entry></env-entry>声明Web应用的环境项。 

<ejb-ref></ejb-ref>声明一个EJB的主目录的引用。 

<ejb-local-ref></ ejb-local-ref>声明一个EJB的本地主目录的应用。   

</web-app>    

2.4 tomcatusers.xml

Store the file of the tomcat user, here is the tomcat user name and password, and the user's role information. You can add tomcat users according to the comment information in the file, and then you can enter the Tomcat Manager page in the Tomcat homepage;

3.lib

Store all the Jar packages required by the Tomcat server.

When we connect to the database, we often worry about introducing a corresponding Oracle Jar package or MySQL Jar every time we create a project. But if you put these two Jar packages in this directory, you can import them once, and you do not need to import the Jar packages every time you create a project later.

The contents of the lib directory are as follows:

LibC> • ®å½

4.logs

The logs directory is used to store the log files generated by tomcat during operation

  • Very important is the log output in the console. (Emptying will not affect the operation of tomcat)
  • In the windows environment, the output log of the console is in the catalina.xxxx-xx-xx.log file. If tomcat fails to start, you can view the log file
  • In the linux environment, the output log of the console is in the catalina.out file

5.temp

The temp directory user stores the temporary files generated by tomcat during the running process. (Emptying will not affect the operation of tomcat)

6.webapps

  • The tomcat default deployment path. The directory is used to store applications. When Tomcat starts, applications in the webapps directory are loaded.
  • Applications can be published in three forms: war package, Jar package, and ordinary folder (class bytecode file).
  • You can also store the application program in any location on the computer disk, but you must configure it in the configuration file to make it mapped.

7.work

  • The work directory is used to store the compiled files of tomcat at runtime, which are generally .java and .class files compiled by JSP.
  • To delete the contents of this directory, the work directory will be generated again when you run it again
  • Clear the work directory and restart tomcat to clear the cache.

Tomcat three application deployment methods

Finally, let me talk about the three application deployment methods of tomcat

  • Implicit deployment (webapp): directly copy the web project file (usually the war package generated by copying) to the webapps directory of tomcat.
  • Explicit deployment (server.xml): In the server.xml file under the conf directory in tomcat, add a context to the node, specifically:
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
        <Context path="/Demo1" docBase="d:/Demo1" reloadable="true"></Context>
</Host>

Among them, reloadable is hot deployment. Generally, we will set the Reloadable attribute to true during the development phase, which is helpful for debugging servlets and other class files. However, because this will increase the operating load of the server and deplete system performance, it is recommended during the project operation phase Set it to false

  • Explicit deployment (path.xml)
  1. In the local tomcat conf directory, create a new Catalina/localhost directory (pay attention to the case of the file name here)
  2. Create a new xml file in this directory, the file name represents path, such as Demo1.xml
  3. Only write Context in xml
<Context docBase="d:/Demo1"  reloadable="true"/>

Guess you like

Origin blog.csdn.net/weixin_43935927/article/details/108605275