Tomcat 服务器(二)—— Tomcat 服务器配置(server.xml、tomcat-user.xml、Web.xml、Tomcat 管理配置、JVM 配置)

一、Tomcat 服务器配置

Tomcat 服务器在使用的时候一般都会进行配置,配置文件主要是conf目录下的catalina.policycatalina.propertiescontext.xmlserver.xmltomcat-user.xmlweb.xml

1、server.xml 文件配置

server.xml是Tomcat服务器的核心配置文件,其包含了Tomcat的Servlet容器(catalina)的所有配置。

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <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>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation. The default
         SSLImplementation will depend on the presence of the APR/native
         library and the useOpenSSL attribute of the
         AprLifecycleListener.
         Either JSSE or OpenSSL style configuration may be used regardless of
         the SSLImplementation selected. JSSE style configuration is used below.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->
    <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
         This connector uses the APR/native implementation which always uses
         OpenSSL for TLS.
         Either JSSE or OpenSSL style configuration may be used. OpenSSL style
         configuration is used below.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
               maxThreads="150" SSLEnabled="true" >
        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
        <SSLHostConfig>
            <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
                         certificateFile="conf/localhost-rsa-cert.pem"
                         certificateChainFile="conf/localhost-rsa-chain.pem"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <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" />

      </Host>
    </Engine>
  </Service>
</Server>

(1)Server

Server是server.xml的根元素,用于创建一个Server实例,默认使用的实现类是org.apache.catalina.core.StandardServer
在这里插入图片描述

  • port:Tomcat监听的关闭服务器的端口
  • shutdown:关闭服务器的指令字符串

Server的内嵌子元素为ListenerGlobalNamingResourcesService

(2)Listener

文件中Listener配置的含义分别如下:

  • ① 用于以日志形式输出服务器、操作系统、JVM的版本信息
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  • ② 用于加载(服务器启动)和销毁(服务器停止)APR,如果找不到APR库,则会输出日志,并不影响Tomcat的启动
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  • ③ 用于避免JRE内存泄漏问题
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  • ④ 用于加载(服务器启动)的销毁(服务器停止)全局命名服务
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

⑤ 用于在Context.xml停止时重建Executor池中的线程,以避免ThreadLocal相关的内存泄露

<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

(3)GlobalNamingResources

GlobalNamingResources中定义了全局命名服务

<GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <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>

(4)Service

  • 该元素用于创建Service实例,默认使用org.apache.catalina.core.standardService。默认情况下Tomcat仅指定了Service的名称,值为Catalina。Service的内嵌子元素有:ListenerExecutorConnectorEngine
  • 一个Server服务器里面可以配置多个Service

(5)Executor

  • Executor用于配置Service共享线程池
  • 默认情况下,Service并未添加共享线程池配置,如果需要配置线程池可以在Service标签中添加如下配置
    <Executor name="tomcatThreadPool" 
    		namePrefix="catalina-exe C- "
    		maxThreads="1000"
    		minSpareThreads="100"
    		maxIdleTime="60000" 
    		maxQueuesize="Integer.MAX VALUE"
    		prestartminSpareThreadg="false"
    		threadPriority="5"
    		className="org. apache. catalina.core. StandardThreadExecutor"/>
    

上述配置的参数说明

  • name:线程池名称,用于Connector中指定。
  • namePrefix:所创建的每个线程的名称前缀,一个单独的线程名称为namePrefix+ threadNumber
  • maxThreads:池中最大线程数。
  • minSpareThreads:活跃线程数,也就是核心池线程数,这些线程不会被销毁,会一直存在。
  • maxIdleTime:线程空闲时间,超过该时间后,空闲线程会被销毁,默认值为6000 ( 1分钟) ,单位毫秒。
  • maxQueueSize:在被执行前最大线程排队数目, 默认为Int的最大值,也就是广义的无限。除非特殊情况,这个值不需要更改,否则会有请求不会被处理的情况发生。
  • prestar tminSpareThreads:启动线程池时是否启动minSpareThreads部分线程。默认值为false,即不启动。
  • threadPriority:线程池中线程优先级,默认值为5,值从1到10。
  • className:线程池实现类,未指定情况下,默认实现类为org. apache. catalina. core . standardThreadExecutor。如果想使用自定义线程池首先需要实现org. apa che. catalina . Executor接口。

(6)Connector

  • Connector用于配置Service包含的链接器,默认情况下, server.xml配置了两个链接器, -一个支持HTTP协议,一个支持AJP协议。因此大多数情况下,我们并不需要新增链接器配置,只是根据需要对已有链接器进行优化。
    <Connector port="8080" protocol="hTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
    
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    

属性说明:
port
端口号,Connector 用于创建服务端socket并进行监听,以等待客户端请求链接。如果该属性设置为0 ,,Tomcat将会随机选择一个可用的端口号给当前Connector使用。

扫描二维码关注公众号,回复: 9370431 查看本文章

protocol
当前Connector 支持的访问协议。 默认为HTTB/1.1 ,并采用自动切换机制选择一 个基于JAVA NIO的链接器或者基于本地APR的链接器(根据本地是否含有Tomcat的本地库判定)。
如果不希望采用.上述自动切换的机制,而是明确指定协议 ,可以使用以下值。

  • Http协议
    org.apache . coyote .http11. Http11NioPxotocol,非阻塞式Java NIO 链接器
    org. apache . coyote .http11 .Http11Nio2Protocol ,非阻塞式 JAVA NIO2链接器
    org. apache . coyote.http11.Http11ApxPxotocol,APR 链接器
    
  • AJP协议
    org. apache.coyote. ajp.AjpNioProtocol ,非阻塞式Java NIO链接器
    org. apache.coyote. ajp.AjpNio2Protocol ,非阻塞式JAVA NIO2 链接器
    org. apache. coyote.ajp.AjpAprProtocol,APR 链接器
    

connectionTimeout
Connector 接收链接后的等待超时时间,单位为毫秒,-1表示不超时。

redirectPort
当前Connector不支持ssI请求, 接收到了一一个请求,并且也符合security-constraint 约束,需要ssI传输,Catalina自动将请求重定向到指定的端口。

executor
指定共享线程池的名称,也可以通过maxThreads. minSpareThreads 等属性配置内部线程池。

URIEncoding
用于指定编码URI的字符编码, Tomcat8. x版本默认的编码为UTE-8。
完整的配置如下:

<Connector port="8080"
		protocol="HTTP/1.1"
		executor="tomcatThreadPool"
		maxThreads="1000" 
		minSpareThreads="100"
		acceptCount="1000"
		maxConnections="1000"
		connectionTimeout="20000"
		compression="on" .
		compres3ionMinSize="2048"
		di 3ableUploadTimeout="true"
		redirectPort="8443"
		URIEncoding="UTF-8" />

(7)Engine

  • Engine用于配置Service中链接器对应的Servlet容器引擎
  • Engine作为servlet引擎的顶级元素,内部可以嵌入:Cluster、 Listener. Realm、 valve和Host。
    <Engine name="Catalina" defaultHost="localhost">
    ...
    </Engine>
    

属性说明:

  • name
    用于指定Engine的名称,默认为Catalina 。该名称会影响一部分Tomcat的存储路径(如临时文件)。
  • defaultHost
    默认使用的虚拟主机名称, 当客户端请求指向的主机无效时,将交由默认的虚拟主机处理,默认为localhost.

(8)Host

  • Host元素用于配置-个虚拟主机,它支持以下嵌入元素 : Alias、Cluster、 Listener、 valve、Realm、Context。 如果在Engine 下配置Realm,那么此配置将在当前Engine下的所有Host中共享。同样 ,如果在Host中配置Realm,则在当前Host 下的所有Context中共享。Context中的Realm优先级> Host 的Realm优先级> Engine中的Realm优先级。
<Host name="localhost" appBase="webappa" unpackWARs="true" autoDeploy="true">
	...
</Host>

属性说明:
name
当前Host通 用的网络名称,必须与DNS服务器上的注册信息一致。Engine中包含的Host必须存在一 个名称与Engine的defaultHost设置一致。
appBase
当前Host的应用基础目录, 当前Host.上部署的web应用均在该目录下(可以是绝对目录,相对路径)。默认为webapps。
unpackWARs
设置为true ,Host在启动时会将appBase目 录下war包解压为目录。设置为false ,Host将直接从war文件启动。
autoDeploy
控制tomcat是否在运行时定期检测并 自动部署新增或变更的web应用。

  • 通过给Host添加别名,我们可以实现同- -个Host拥有多个网络名称,配置如下:
    <Host name="www. web1.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
    	<Alia3>www.web2.com</Alia3>
    </Host>
    

此时可以通过两个域名访问当前Host下的应用(需要确保DNs或hosts中添加了域名的映射配置)。

(9)Context

  • Context用于配置-个web应用,默认的配置如下:
    <Context docBase="myApp" path="/myApp">
    	...
    </Context>
    

属性说明

  • docBase
    Web应用目录或者War包的部署路径。可以是绝对路径,也可以是相对于Host appBase的相对路径。
  • path
    Web应用的Context路径。如果我们Host名为1ocalhost,则该web应用访问的根路径为 http://localhost:8080/myApp
    它支持的内嵌元素为: CookieProcessorLoaderManager , Realm, Resources , WatchedResource , JarScanner, Valve
    <Host name="www. tomcat.com" appBase="webappa" unpackWARs="true" autoDeploy="true">
    	
    	<Context docBase="D: \servlet_ project03" path="/myApp"></Context>
    	
    	<Valve cla33Name="org. apache.catalina. valves.AccessLogValve" directory="logs"
    						prefix="localhost_ access_ log" suffix=".txt"
    						pattern="&h &l &u &t &quot;&r&quot; &s &b" />
    </Host>
    

2、tomcat-user.xml配置文件

该配置文件中,主要配置的是Tomcat的用户、角色等信息,用来控制Tomcat中的Manager、host-manager的访问权限。

二、Tomcat 应用配置

1、Web.xml 配置文件

web. xmT是web应用的描述文件,它支持的元素及属性来自于servlet规范定义。在Tomcat 中,web 应用的描述信息包括tomcat/ conf/web. xml中默认配置以及Web应用WEB-INE/web . xml下的定制配置。

welcome- file-list								http:/ /xmlns. jcp.org/xm1/ns/javaee
servlet -mapping								http://xmlns.jcp.org/xml/ns/javaee
servlet												http://xmlns.jcp.org/xm1/ns/javaee
filter- mapping									http://xmlns.jcp.org/xm1/ns/javaee
filter													http://xmlns.jcp.org/xm1/ns/javaee
listener											http://xmlns.jcp.org/xml/n3/javaee
context-param									http://xmlns.jcp.org/xml/ns/javaee
absolute-ordering							http://xmlns .jcp .org/xm1/ns/javaee
administered-object							http://xmlns.jcp.org/xml/ns/javaee
connection- factory							http://xmlns.jcp.org/xm1/ns/javaee
data-source										http:/ /xmln3. jcp.org/xml/na/javaee
deny-uncovered-http -methods		http://xcmlns.jcp.org/xm1/ns/javaee
description										http://xmln3.jcp.org/xml/ns/javaee
display -name									http://xmlns.jcp.org/xm1/ns/javaee
distributable										http://xmlng .jcp .org/xm1/na/javaee
ejb-local-ref										http://xmlns. jcp.org/xml/n3/javaee
ejb- ref												http://xmIns .jcp .org/ xml/ns/javaee
env-entry											http://xmlns.jcp.org/xml/ns/javaee
error- -page										http://xmlns.jcp.org/xml/ns/javaee
icon													http://xmlns.jcp.org/xml/ns/javaee
jms-connection factory					http://xmlns.jcp.org/xm1/ns/javaee
jms-destination								http:/ /xmlns.jcp.org/xml/na/javaee
jsp-config											http://xmlns.jcp.org/xm1/n3/javaee
locale-encoding -mapping-list			http://xmIns .jcp .org/xml/ns/javaee
login-config										http://xmlns.jcp.org/xml/ns/javaee
mail- 3essionA 								http://xmlns .jcp .org/ xml/ns/javaee
message -destination						http://xcmlns.jcp.org/xm1/ns/javaee
message- destination-ref\				http://xmlns.jcp.org/xm1/ns/javaee
mime -mapping								http://xmlns.jcp.org/xml/ns/javaee
module-name									http://xmlns.jcp.org/xml/ns/javaee
persistence-context-ref 					http://xmlns.jcp.org/xml/ns/javaee
persistence -unit- ref						http://xmlns. jcp.org/xm1/n3/javaee
post- construct								http:/ /xmlns.jcp.org/xml/ns/javaee
pre-destroy										http://xmlns. jcp.org/xml/na/javaee
resource-envy - ref							http://xmlns.jcp.org/xml/ns/javaee
resource- ref									http:/ /xmlns.jcp.org/xml/n3/javaee
security-constraint							http://xmlns.jcp.org/xml/ns/javaee
security- - role									http://xmlns.jcp.org/xml/ns/javaee
service-ref										http:/ /xmln3. jcp.org/xm1/ns/javaee
session-config									http://xmlns. jcp .org/ xml/ns/javaee

(1)ServletContext 初始化参数

通过<context-param>可以添加servletContext 初始化参数,它配置了一个键值对,这样我们可以在应用程序中使用javax. servlet . ServletContext . getInitParameter ()方法获取参数。

<context-par am>
	<param- name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext-- .xm1</param-value>
	<description>Spring Config Flle Location</description>
</context-param>

(2)会话配置

<session-config>用于配置web应用会话,包括超时时间、Cookie配置以及会话追踪模式,它将覆盖server.xmlcontext . xml中的配置。

<sesion-config>
	<ses3ion-timeout>30</ session-timeout>
	<cookie-config>
		<name>JESSIONID</ name >
		<domain>www. itcast.cn</ domain>
		<path>/</path>
		<comment>Session Cookie</ comment>
		<http-only>true</http-only>
		<secure>false</ secure>
		<max-age>3600</max-age>
	</cookie-config>
	<tracking - -mode>COOKIE</tracking-mode>
</3ession-config>

参数说明

  • session-timeout :会话超时时间,单位分钟
  • cookie-config:用于配置会话追踪Cookie
    • name:Cookie的名称
    • domain:Cookie的域名
    • path:Cookie的路径
    • comment:注释
    • http-only:cookie只能通过HTTP方式进行访问,Js无法读取或修改,此项可以增加网站访问的安全性。
    • secure:此cookie只能通过HTTes连接传递到服务器,而HTTP连接则不会传递该信息。注意是从浏览器传递到服务器,服务器端的Cookie对象不受此项影响。
    • max-age:以秒为单位表示cookie的生存期,默认为-1表示是会话Cookie,浏览器关闭时就会消失。
  • tracking-mode:用于配置会话追踪模式,Servlet3.0版本中支持的追踪模式:COOKIE、 URL、 SSL
    • COOKIE:通过HTTP Cookie追踪会话是最常用的会话追踪机制,而且servlet规范也要求所有的Servlet规范都需要 支持Cookie追踪。
    • URJ:URI重写是最基本的会话追踪机制。当客户端不支持Cookie时,可以采用URI重写的方式。当采用URL追踪模式时,请求路径需要包含会话标识信息,Servlet容器会根据路径中的会话标识设置请求的会话信息。如:http : //www . myserver. com/user/index. html;jessionid=1234567890
    • SSL:对于SSL请求, 通过SSL会话标识确定请求会话标识。

(3)Servlet 配置

Servlet的配置主要是两部分,servletservlet-mapping

<servlet>
	<3exvlet- name>mySexvlet</gexvlet-name>
	<servlet-class>cn.itcast .web .MyServlet</servlet-c1a33>
	<init-param>
		<par am- name>fl1eName</param-name>
		<param-value>init.conf</param-value>
	</init -param>
	<load-on-startup>1</load-on-startup>
	<enabled>true</enabled>
</servlet>

<servlet-mapping>
	<3exvlet-name>myServlet</sexvlet-name>
	<url-pattern>* .do</url-pattern>
	<url-pattern>/myservet/*</url-pattern>
</servlet -mapping>

配置说明

  • servlet-name:指定servlet的名称, 该属性在web. xml中唯一。
  • servlet-claas:用于指定servlet类名
  • init-param:用于指定servlet的初始化参数, 在应用中可以通过HttpServlet. getInitParameter 获取。
  • load-on-startup:用于控制在Web应用启动时,Servlet的加载顺序。 值小于0,web应用启动时,不加载该servlet, 第一-次访
    问时加载。
  • enabled:true ,false 。若为false ,表示Servlet不处理任何请求。

(4)Servlet 配置

Listener用于监听servlet中的事件,例如context、request、 session对象的创建、 修改、删除,并处罚响应事件。
Listener是观察者模式的实现,在servlet中主要用于对context、request、 session对象的生命周期进行监控。
在servlet2 .5规范中共定义了8种Listener。在启动时, servletContextListener的执行顺序与web. xml中的配置顺序-致,停止时执行顺序相反。

<listener>
	<listener-clas3>org. springframework . web. context. ContextLoaderListener</listener-cla33>
</listener>

(5)Filter 配置

filter用于配置web应用过滤器,用来过滤资源请求及响应。经常用于认证、 日志加密、数据转换等操作,配置如下 :

<filter>
	<filter-name>myFilter</ filter-name>
	<filter-class>cn.itcast.web.MyFilter</filter-cla33>
	<async- supported>true</async- supported>
	<init-param>
		<param-name>language</param-name>
		<param-value>CN</param-value>
	</ init-param>
</ filter>
<filter-mapping>
	<filter -name>myFilter</ filter-name>
	<url -pattern>/*</url-pattern>
</ filter -mapping>

配置说明

  • filter-name:用于指定过滤器名称,在web. xml1中,过滤器名称必须唯一。
  • filter-class:过滤器的全限定类名,该类必须实现Filter接口。
  • async-supported:该过滤器是否支持异步
  • init-param:用于配置Filter的初始化参数,可以配置多个,可以通过FilterConfig. getInitParameter获取
  • url-pattern:指定该过滤器需要拦截的URL。

(6)欢迎页面配置

welcome-file-list用于指定web应用的欢迎文件列表。

<welcome- file-list>
	<welcome- file>index.html</welcome-file>
	<welcome-file>index. htm</welcome- file>
	<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

(7)错误页面配置

error-page用于配置web应用访问异常时定向到的页面,支持HTTP响应码和异常类两种形式。

<error-page>
	<error-code>404</error-code>
	<location>/404. html</location>
</error-page>
<error-page>
	<error-code>500</error-code>
	<location>/500.html</location>
</error-page>
<error-page>
	<exception-type>java.lang. Exception</exception-type>
	<location>/error.jsp</location>
< /error-page>

三、Tomcat 管理配置

从早期的Tomcat版本开始,就提供了Web版的管理控制台,他们是两个独立的web应用,位于webapps目录下。Tomcat提供的管理应用有用于管理的Host的host-manager和用于管理web应用的manager。

1、host-manager

Tomcat启动之后,可以通过http://localhost:8080/host- -manager/html访问该web应用。host-manager 默认添加了访问权限控制,当打开网址时,需要输入用户名和密码(conf/tomcat-users . xml中配置)。 所以要想访问该页面,需要在conf/tomcat-users.xml中配置,并分配对应的角色:

  • admin-gui:用于控制页面访问权限
  • admin-script`:用于控制以简单文本的形式进行访问

配置如下:

<role rolename="admin-gui"/>
<role rolename="admin 3cript"/>
<user username="itcast" password="itcast" roles="admin-script, admin-gui"/>

2、manager

manager的访问地址为http: //localhost:8080/manager,同样,manager也添加 了页面访问控制,因此我们需要为登录用户
分配角色为:

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="itcast" password="itcast" roles="admin-script, admin-gui , manager-gui , manager-script"/>

四、JVM 配置

最常见的JVM配置当属内存分配,因为在绝大多数情况下,JVM默认分配的内存可能不能够满足我们的需求,特别是在生产环境,此时需要手动修改Tomcat启动时的内存参数分配。

1、JVM内存模型图

在这里插入图片描述

2、JVM配置选项

  • windows平台(catalina.bat)
set JAVA_ OPTS=-server  -Xms2048m  -Xmx2048m  -XX:MetaspaceSize=256m -XX:MaxMetaspacesize=256m  -XX: SurvivorRatio=8
  • linux平台(catalina.sh)
JAVA_ OPTS="-server  -Xms1024m  -Xmx2048m  -XX:MetaspaceSize=256m  -XX:MaxMetaspacesize=512m  -XX: SurvivorRatio=8"
参数 含义
-Xms 堆内存的初始大小
-Xmx 堆内存的最大大小
-Xmn 新生代的内存大小,官方建议是整个堆得3/8
-XX:Metaspacesize 元空间内存初始大小,在JDK1 .8版本之前配置为-XX: Permsize (永久代)
-XX:MaxMetaspacesize 元空间内存最大大小,在JDK1. 8版本之前配置为-XX:MaxPermSize (永久代)
-XX:Ini tialCodeCacheSize
-XX:ReservedCodeCachesize
代码缓存区大小
-XX:NewRatio 设置新生代和老年代的相对大小比例。这种方式的优点是新生代大小会随着整个堆大小动态扩展。如-XX:NewRatio=3 指定老年代/新生代为3/1。老年代占堆大小的3/4,新生代占1/4。
-XX:SurvivorRatio 指定伊甸园区(Eden) 与幸存区大小比例。 如-XX:SurvivorRatio=10 表示伊甸园区(Eden) 是幸存区To大小的10倍(也是幸存区From的10倍)。所以,伊甸园区(Eden) 占新生代大小的10/12,幸存区From和幸存区To每个占新生代的1/12。注意, 两个幸存区永远是一 样大
发布了40 篇原创文章 · 获赞 0 · 访问量 340

猜你喜欢

转载自blog.csdn.net/baidu_27414099/article/details/104439663