Quick Start Tomcat

Brief introduction

What is Tomcat

Tomcat is a Servlet container Apache developed, implemented support for Servlet and JSP, and offers some unique features as Web servers, such as Tomcat management and control platform, security domain management, and Tomcat valve.

Because Tomcat itself contains an HTTP server, it can also be regarded as a separate Web server. However, it can not be confused with Tomcat and Apache HTTP Server, Apache HTTP server is a HTTP Web server using the C language; these two HTTP web server is not bundled together. Tomcat includes a configuration management tool that can be configured by editing XML configuration file format.

Tomcat important directories

  • / bin  - Tomcat script storage directory (such as startup, shutdown script). *.sh For Unix file system;  *.bat file system for Windows.
  • / conf  - Tomcat configuration files.
  • / logs  - Tomcat default log directory.
  • / webapps  - run the webapp directory.

web publishing project directory structure

General web project directory structure

|-- webapp                         # 站点根目录
    |-- META-INF                   # META-INF 目录
    |   `-- MANIFEST.MF            # 配置清单文件
    |-- WEB-INF                    # WEB-INF 目录 | |-- classes # class文件目录 | | |-- *.class # 程序需要的 class 文件 | | `-- *.xml # 程序需要的 xml 文件 | |-- lib # 库文件夹 | | `-- *.jar # 程序需要的 jar 包 | `-- web.xml # Web应用程序的部署描述文件 |-- <userdir> # 自定义的目录 |-- <userfiles> # 自定义的资源文件

webapp: Project Publish folder. In fact, every package can be regarded as war webapp archive.

META-INF: META-INF directory used to store some information related to itself, Metafile Information Engineering, generally, the environment automatically generated by development tools.

WEB-INF: Security catalog Java web applications. The so-called security is the client can not access, only the directory server can access.

/WEB-INF/classes: All Java class files are stored procedures needed.

/WEB-INF/lib: All jar files are stored procedures needed.

/WEB-INF/web.xml: Deployment profile web applications. It is the most important project in the configuration file, which describes the servlet and other components make up the application, and the application initialization parameters, security management constraints.

installation

Prerequisites

Tomcat 8.5 requires JDK 1.7 or higher.

Into the  Tomcat official download address  to select the appropriate version to download and extract to a local.

Windows

Add environmental variables  CATALINA_HOME , the value of the installation path of Tomcat.

bin directory into the installation directory, run the startup.bat file, start Tomcat

Linux / Unix

The following example to 8.5.24 version, for example, contains the download, unzip, start-up operation.

# 下载解压到本地
wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz
tar -zxf apache-tomcat-8.5.24.tar.gz
# 启动 Tomcat
./apache-tomcat-8.5.24/bin/startup.sh

Once started, visit  HTTP: // localhost: 8080  , you can see the Tomcat installation successful test page.

tomcat.png

Configuration

This section will list some important, common configuration items. Detailed Tomcat8 configuration can refer to  Tomcat 8 Configuration official reference documentation  .

Server

Server element represents the entire Catalina servlet container.

Therefore, it must be  conf/server.xml the root element in the configuration file. Its properties represent the characteristics of the entire servlet container.

Property sheet

Attributes description Remark
className This class must implement org.apache.catalina.Server interface. Default org.apache.catalina.core.StandardServer
address Server shutdown command waits for a TCP / IP address. If you do not specify an address, use localhost.  
port The server waits for TCP / IP port number for the shutdown command. Set to -1 to disable closed port.  
shutdown Receiving a command string to be connected to the port number specified by the TCP / IP, to close the Tomcat.  

Service

Service element represents one or more combinations of the connector assembly, these components share an engine component for processing incoming requests. Server can have multiple Service.

Property sheet

Attributes description Remark
className This class must implement the org.apache.catalina.Serviceinterface. default org.apache.catalina.core.StandardService
name 此服务的显示名称,如果您使用标准 Catalina 组件,将包含在日志消息中。与特定服务器关联的每个服务的名称必须是唯一的。  

实例 - conf/server.xml 配置文件示例

<?xml version="1.0" encoding="UTF-8"?> <Server port="8080" shutdown="SHUTDOWN"> <Service name="xxx"> ... </Service> </Server>

Executor

Executor表示可以在Tomcat中的组件之间共享的线程池。

属性表

属性 描述 备注
className 这个类必须实现org.apache.catalina.Executor接口。 默认 org.apache.catalina.core.StandardThreadExecutor
name 线程池名称。 要求唯一, 供Connector元素的executor属性使用
namePrefix 线程名称前缀。  
maxThreads 最大活跃线程数。 默认200
minSpareThreads 最小活跃线程数。 默认25
maxIdleTime 当前活跃线程大于minSpareThreads时,空闲线程关闭的等待最大时间。 默认60000ms
maxQueueSize 线程池满情况下的请求排队大小。 默认Integer.MAX_VALUE
<Service name="xxx"> <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="300" minSpareThreads="25"/> </Service>

Connector

Connector代表连接组件。Tomcat 支持三种协议:HTTP/1.1、HTTP/2.0、AJP。

属性表

属性 说明 备注
asyncTimeout Servlet3.0规范中的异步请求超时 默认30s
port 请求连接的TCP Port 设置为0,则会随机选取一个未占用的端口号
protocol 协议. 一般情况下设置为 HTTP/1.1,这种情况下连接模型会在NIO和APR/native中自动根据配置选择  
URIEncoding 对URI的编码方式. 如果设置系统变量org.apache.catalina.STRICT_SERVLET_COMPLIANCE为true,使用 ISO-8859-1编码;如果未设置此系统变量且未设置此属性, 使用UTF-8编码
useBodyEncodingForURI 是否采用指定的contentType而不是URIEncoding来编码URI中的请求参数  

以下属性在标准的Connector(NIO, NIO2 和 APR/native)中有效:

属性 说明 备注
acceptCount 当最大请求连接maxConnections满时的最大排队大小 默认100,注意此属性和Executor中属性maxQueueSize的区别.这个指的是请求连接满时的堆栈大小,Executor的maxQueueSize指的是处理线程满时的堆栈大小
connectionTimeout 请求连接超时 默认60000ms
executor 指定配置的线程池名称  
keepAliveTimeout keeAlive超时时间 默认值为connectionTimeout配置值.-1表示不超时
maxConnections 最大连接数 连接满时后续连接放入最大为acceptCount的队列中. 对 NIO和NIO2连接,默认值为10000;对 APR/native,默认值为8192
maxThreads 如果指定了Executor, 此属性忽略;否则为Connector创建的内部线程池最大值 默认200
minSpareThreads 如果指定了Executor, 此属性忽略;否则为Connector创建线程池的最小活跃线程数 默认10
processorCache 协议处理器缓存Processor对象的大小 -1表示不限制.当不使用servlet3.0的异步处理情况下: 如果配置Executor,配置为Executor的maxThreads;否则配置为Connnector的maxThreads. 如果使用Serlvet3.0异步处理, 取maxThreads和maxConnections的最大值

Context

Context元素表示一个Web应用程序,它在特定的虚拟主机中运行。每个Web应用程序都基于Web应用程序存档(WAR)文件,或者包含相应的解包内容的相应目录,如Servlet规范中所述。

属性表

属性 说明 备注
altDDName web.xml部署描述符路径 默认 /WEB-INF/web.xml
docBase Context的Root路径 和Host的appBase相结合, 可确定web应用的实际目录
failCtxIfServletStartFails 同Host中的failCtxIfServletStartFails, 只对当前Context有效 默认为false
logEffectiveWebXml 是否日志打印web.xml内容(web.xml由默认的web.xml和应用中的web.xml组成) 默认为false
path web应用的context path 如果为根路径,则配置为空字符串(""), 不能不配置
privileged 是否使用Tomcat提供的manager servlet  
reloadable /WEB-INF/classes/ 和/WEB-INF/lib/ 目录中class文件发生变化是否自动重新加载 默认为false
swallowOutput true情况下, System.out和System.err输出将被定向到web应用日志中 默认为false

Engine

Engine元素表示与特定的Catalina服务相关联的整个请求处理机器。它接收并处理来自一个或多个连接器的所有请求,并将完成的响应返回给连接器,以便最终传输回客户端。

属性表

属性 描述 备注
defaultHost 默认主机名,用于标识将处理指向此服务器上主机名称但未在此配置文件中配置的请求的主机。 这个名字必须匹配其中一个嵌套的主机元素的名字属性。
name 此引擎的逻辑名称,用于日志和错误消息。 在同一服务器中使用多个服务元素时,每个引擎必须分配一个唯一的名称。

Host

Host元素表示一个虚拟主机,它是一个服务器的网络名称(如“www.mycompany.com”)与运行Tomcat的特定服务器的关联。

属性表

属性 说明 备注
name 名称 用于日志输出
appBase 虚拟主机对应的应用基础路径 可以是个绝对路径, 或CATALINABASE||xmlBase|XML,Contextxml|,CATALINABASE相对路径||xmlBase|虚拟主机XML基础路径,里面应该有Contextxml配置文件|可以是个绝对路径,或{CATALINA_BASE}相对路径
createDirs 当appBase和xmlBase不存在时,是否创建目录 默认为true
autoDeploy 是否周期性的检查appBase和xmlBase并deploy web应用和context描述符 默认为true
deployIgnore 忽略deploy的正则  
deployOnStartup Tomcat启动时是否自动deploy 默认为true
failCtxIfServletStartFails 配置为true情况下,任何load-on-startup >=0的servlet启动失败,则其对应的Contxt也启动失败 默认为false

Cluster

由于在实际开发中,我从未用过Tomcat集群配置,所以没研究。

启动

部署方式

这种方式要求本地必须安装 Tomcat 。

将打包好的 war 包放在 Tomcat 安装目录下的 webapps 目录下,然后在 bin 目录下执行 startup.bat 或 startup.sh ,Tomcat 会自动解压 webapps 目录下的 war 包。

成功后,可以访问 http://localhost:8080/xxx (xxx 是 war 包文件名)。

注意

以上步骤是最简单的示例。步骤中的 war 包解压路径、启动端口以及一些更多的功能都可以修改配置文件来定制 (主要是 server.xml 或 context.xml 文件)。

嵌入式

API 方式

在 pom.xml 中添加依赖

<dependency>
  <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>8.5.24</version> </dependency>

添加 SimpleEmbedTomcatServer.java 文件,内容如下:

import java.util.Optional;
import org.apache.catalina.startup.Tomcat;

public class SimpleTomcatServer { private static final int PORT = 8080; private static final String CONTEXT_PATH = "/javatool-server"; public static void main(String[] args) throws Exception { // 设定 profile Optional<String> profile = Optional.ofNullable(System.getProperty("spring.profiles.active")); System.setProperty("spring.profiles.active", profile.orElse("develop")); Tomcat tomcat = new Tomcat(); tomcat.setPort(PORT); tomcat.getHost().setAppBase("."); tomcat.addWebapp(CONTEXT_PATH, getAbsolutePath() + "src/main/webapp"); tomcat.start(); tomcat.getServer().await(); } private static String getAbsolutePath() { String path = null; String folderPath = SimpleEmbedTomcatServer.class.getProtectionDomain().getCodeSource().getLocation().getPath() .substring(1); if (folderPath.indexOf("target") > 0) { path = folderPath.substring(0, folderPath.indexOf("target")); } return path; } }

成功后,可以访问 http://localhost:8080/javatool-server 。

说明

本示例是使用 org.apache.tomcat.embed 启动嵌入式 Tomcat 的最简示例。

这个示例中使用的是 Tomcat 默认的配置,但通常,我们需要对 Tomcat 配置进行一些定制和调优。为了加载配置文件,启动类就要稍微再复杂一些。这里不想再贴代码,有兴趣的同学可以参考:

示例项目

使用 maven 插件启动(不推荐)

不推荐理由:这种方式启动 maven 虽然最简单,但是有一个很大的问题是,真的很久很久没发布新版本了(最新版本发布时间:2013-11-11)。且貌似只能找到 Tomcat6 、Tomcat7 插件。

使用方法

在 pom.xml 中引入插件

<plugin>
  <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8080</port> <path>/${project.artifactId}</path> <uriEncoding>UTF-8</uriEncoding> </configuration> </plugin>

运行 mvn tomcat7:run 命令,启动 Tomcat。

成功后,可以访问 http://localhost:8080/xxx (xxx 是 ${project.artifactId} 指定的项目名)。

IDE 插件

常见 Java IDE 一般都有对 Tomcat 的支持。

以 Intellij IDEA 为例,提供了 Tomcat and TomEE Integration 插件(一般默认会安装)。

使用步骤

  • 点击 Run/Debug Configurations > New Tomcat Server > local ,打开 Tomcat 配置页面。
  • 点击 Confiure... 按钮,设置 Tomcat 安装路径。
  • 点击 Deployment 标签页,设置要启动的应用。
  • 设置启动应用的端口、JVM 参数、启动浏览器等。
  • 成功后,可以访问 http://localhost:8080/(当然,你也可以在 url 中设置上下文名称)。

tomcat-intellij-run-config.png

说明

个人认为这个插件不如 Eclipse 的 Tomcat 插件好用,Eclipse 的 Tomcat 插件支持对 Tomcat xml 配置文件进行配置。而这里,你只能自己去 Tomcat 安装路径下修改配置文件。

资料

Guess you like

Origin www.cnblogs.com/canflyfish/p/11580671.html