---
title: "Tomcat 整理"
author: "jay1986"
date: "2010-11-08T17:41:29Z"
tags: []
categories: ["企业架构"]
slug: "tomcat"
draft: false
lastmod: "2010-11-08T17:41:29Z"
id: 805851
url: "https://jay1986.iteye.com/blog/805851"
---

[Tomcat 整理](/blog/145770)

**1.apr**  
许多朋友可能在启动tomcat的时候都会看到类似这样的信息：

引用

org.apache.catalina.core.AprLifecycleListener init  
信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\\Java\\jre\\bin;.;C:\\WINDOWS\\Sun\\Java\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS

出现这种情况是这表示没有找到APR  
简要解决办法：去 [http://tomcat.heanet.ie/native/](http://tomcat.heanet.ie/native/) 下载编译好的tcnative-1.dll文件，目前最新为1.1.14,拷贝至jdk\\bin下，再启动就可以成功加载APR了。

引用

org.apache.catalina.core.AprLifecycleListener init  
信息: Loaded Apache Tomcat Native library 1.1.14.  
org.apache.catalina.core.AprLifecycleListener init  
信息: APR capabilities: IPv6 \[false], sendfile \[true], accept filters \[false], random \[true].

**2.URIEncoding**  
有时候在做开发的时候经常发现文本框输入的中文到了程序中成了乱码，其实是因为在端口监听部分缺少编码。

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. URIEncoding="UTF-8"

```
 URIEncoding="UTF-8" 
```

解决方法如下：  
原始部分  
8080端口上

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. **&lt;Connector** port="8080" protocol="HTTP/1.1"
2. connectionTimeout="20000"
3. redirectPort="8443" **/&gt;**

```
<Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
```

修改后

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. **&lt;Connector** port="8080" protocol="HTTP/1.1"
2. connectionTimeout="20000"
3. redirectPort="8443" URIEncoding="UTF-8" **/&gt;**

```
<Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" URIEncoding="UTF-8" />
```

8009端口 ajp跳转服务上，关于这个端口在apache http 做跳转时，要相当注意

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. **&lt;Connector** port="8009" protocol="AJP/1.3" redirectPort="8443" **/&gt;**

```
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
```

修改后

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. **&lt;Connector** port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" **/&gt;**

```
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />
```

这样，服务器得到的中文字符就不会再有乱码了。

**3.设置Tomcat管理员帐号**  
修改tomcat-users.xml文件，在**&lt;/tomcat-users&gt;**的标签前添加一行

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. **&lt;user** username="tomcat" password="tomcat" roles="admin,manager"**/&gt;**

```
<user username="tomcat" password="tomcat" roles="admin,manager"/>
```

让tomcat用户拥有管理员权限。

**4.设置SSL**  
首先，我们要创建密钥：

Shell代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. keytool -genkey -alias tomcat -keyalg RSA

```
keytool -genkey -alias tomcat -keyalg RSA
```

此时，用户主目录下会生成一个**.keystore**文件。  
然后，我们配置server.xml文件，找到SSLEnabled="true"所在的标签，将其解除注释，同时填补两个属性：  
    1.keystoreFile="C:/Users/Zlex/.keystore"  
    2.keystorePass="123456"  
keystoreFile 指的是你的密钥文件存储的路径，keystorePass指的是你的密码。  
举例如下：

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. &lt;!--
02. Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the
03. JSSE configuration, when using APR, the connector should be using the
04. OpenSSL style configuration described in the APR documentation
05. --**&gt;**
06. &lt;!-- --&gt;
07. **&lt;Connector**
08. SSLEnabled="true"
09. clientAuth="false"
10. keystoreFile="C:/Users/Zlex/.keystore"
11. keystorePass="123456"
12. maxThreads="150"
13. port="8443"
14. protocol="HTTP/1.1"
15. scheme="https"
16. secure="true"
17. sslProtocol="TLS" **/&gt;**

```
		<!--
			Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the
			JSSE configuration, when using APR, the connector should be using the
			OpenSSL style configuration described in the APR documentation
		-->
		<!--	-->
		<Connector
			SSLEnabled="true"
			clientAuth="false"
			keystoreFile="C:/Users/Zlex/.keystore"
			keystorePass="123456"
			maxThreads="150"
			port="8443"
			protocol="HTTP/1.1"
			scheme="https"
			secure="true"
			sslProtocol="TLS" />
```

最后，重启tomcat，在地址栏中访问 [https://localhost:8443/](https://localhost:8443/)。  
将上述**port="8443"**配置改为**port="443"**，可以通过[https://localhost/](https://localhost/)直接访问。  
需要双向认证？参考如下内容：

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. **&lt;Connector** port="443"
02. URIEncoding="UTF-8"
03. useBodyEncodingForURI="true"
04. maxHttpHeaderSize="33192"
05. maxThreads="150"
06. minSpareThreads="25"
07. maxSpareThreads="75"
08. enableLookups="false"
09. disableUploadTimeout="true"
10. acceptCount="100"
11. scheme="https"
12. secure="true"
13. SSLEnabled="true"
14. clientAuth="true"
15. keystoreFile="conf/server.keystore"
16. keystorePass="123456"
17. truststoreFile="conf/ca.p12"
18. truststorePass="123456"
19. truststoreType="PKCS12"
20. sslProtocol="TLS" **/&gt;**

```
        <Connector port="443" 
        	URIEncoding="UTF-8" 
        	useBodyEncodingForURI="true" 
        	maxHttpHeaderSize="33192"
            maxThreads="150" 
           	minSpareThreads="25" 
           	maxSpareThreads="75"
            enableLookups="false"
            disableUploadTimeout="true" 
            acceptCount="100"
            scheme="https" 
            secure="true" 
            SSLEnabled="true" 
            clientAuth="true"
            keystoreFile="conf/server.keystore" 
            keystorePass="123456"
            truststoreFile="conf/ca.p12" 
            truststorePass="123456"
            truststoreType="PKCS12"
            sslProtocol="TLS" /> 
```

其中，

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. clientAuth="true"
2. keystoreFile="conf/server.keystore"
3. keystorePass="123456"
4. truststoreFile="conf/ca.p12"
5. truststorePass="123456"
6. truststoreType="PKCS12"

```
            clientAuth="true"
            keystoreFile="conf/server.keystore" 
            keystorePass="123456"
            truststoreFile="conf/ca.p12" 
            truststorePass="123456"
            truststoreType="PKCS12"
```

**clientAuth="true"**开启双向认证  
**keystoreFile="conf/server.keystore"** 指向服务器密钥库  
**keystorePass="123456"** 服务器密钥库密码  
**truststoreFile="conf/ca.p12"**指向CA信任库  
**truststorePass="123456"**CA信任库密码  
**truststoreType="PKCS12"**CA信任库格式，除了PKCS#12还有JKS，JKS为java原生默认支持的密钥库格式！  
更多ssl配置访问[http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html](http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html)。

**5.通过GZIP压缩加速服务器响应速度**

只需要配置：

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. **&lt;Connector**
02. port="8080"
03. protocol="HTTP/1.1"
04. connectionTimeout="20000"
05. redirectPort="443"
06. URIEncoding="UTF-8"
07. compression="on"
08. noCompressionUserAgents="gozilla, traviata"
09. compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,application/json"
10. **/&gt;**

```
 <Connector 
               port="8080" 
               protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="443" 
               URIEncoding="UTF-8"
               compression="on"   
               noCompressionUserAgents="gozilla, traviata"   
               compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,application/json" 
 />  
```

说说配置细节：  
**compression="on"   开启压缩支持**  
**noCompressionUserAgents="gozilla, traviata"   不压缩的内容**  
**compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,application/json" 压缩的类型**

之后进行的访问均可获得GZIP压缩支持![](/images/smiles/icon_biggrin.gif)

**6.设置静态页面编码**  
修改web.xml  
加入如下内容，是\*.hml、\*.html静态页面默认字符集编码为UTF-8

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. **&lt;mime-mapping&gt;**
2. **&lt;extension&gt;**htm**&lt;/extension&gt;**
3. **&lt;mime-type&gt;**text/html;charset=utf-8**&lt;/mime-type&gt;**
4. **&lt;/mime-mapping&gt;**
5. **&lt;mime-mapping&gt;**
6. **&lt;extension&gt;**html**&lt;/extension&gt;**
7. **&lt;mime-type&gt;**text/html;charset=utf-8**&lt;/mime-type&gt;**
8. **&lt;/mime-mapping&gt;**

```
	<mime-mapping>
		<extension>htm</extension>
		<mime-type>text/html;charset=utf-8</mime-type>
	</mime-mapping>
	<mime-mapping>
		<extension>html</extension>
		<mime-type>text/html;charset=utf-8</mime-type>
	</mime-mapping>
```

本文转自 [http://snowolf.iteye.com/blog/145770](http://snowolf.iteye.com/blog/145770)