Tomcat是Java领域最著名的Web开源容器,简单,易用,稳定性好。既可以用于个人学习使用,也可以作为商业开发产品发布。Tomcat不仅仅提供Web容器的基本功能,还支持JAAS和JNDI绑定等。目前最新的发布版本Tomcat是8.0.14,这个文章中使用的Tomcat版本是8.0.3,其实最新版与该版本的区别不是非常大,具体有哪些新特性可以阅读apache的官方文档,进行了解。


  1. 下载Tomcat服务器

登录http://tomcat.apache.org下载。

解压安装包

解压后会得到如下文件结构:

bin:存放启动和关闭Tomcat的命令路径。

conf:存放Tomcat的配置文件

lib:存放Tomcat服务器的核心类(JAR文件)如果要扩展TOmcat的功能可以将第三方类库拷贝到该目录下。

logs:这个是一个空目录,该目录用于保存每次运行Tomcat而产生的日志。

temp:保存Web应用运行过程中生产的临时文件。

webapps:该目录用于自动部署Web应用的目录,将Web应用复制到该目录下,toncat将会自动部署在容器中。

work:保存web应用运行中,编译生产的class文件,该文件夹可以删除,但是每次启动Tomcat服务器时会自动创建该目录。

LICENSE等相关文档:

将解压后的文件夹放在任意的路径下,运行Tomcat只需要一个环境变量:JAVA_HOME,不管是Windows,还是Linux只需要添加一个环境变量即可,该环境变量指向JDK的安装路径。

Tomcat的下载、安装、配置、管理_最新版

启动Tomcat服务器

在Windows下只需要双击Tomcat安装路径下的bin下的startup.bat就可以了。然后打开浏览器,在地址栏输入http://localhost:8080/ 按下回车键出现如下页面则表示TOmcat服务器安装成功。

 Tomcat的下载、安装、配置、管理_Tomcat_02                                                                                                           

配置Tomcat服务器端口

Tomcat的服务端口默认是8080,可以通过修改配置文件来改变服务端口,控制台等。Tomcat可以通过配置文件同时多个端口提供服务。

通过修改server.xml文件可以改变Tomcat的配置。


Tomcat的下载、安装、配置、管理_服务器_03

Tomcat的下载、安装、配置、管理_Tomcat_04

Tomcat的下载、安装、配置、管理_服务器_05

进入控制台

Tomcat有三个控制台:Server Status控制台:用于监控服务器状态。

Manager App控制台:用于部署、监控Weby应用。Host Manager控制台。

Tomcat的下载、安装、配置、管理_服务器_06

通常我们使用Manager控制台就可以了,这个控制台需要用户和密码才能登录,控制台的用户名和密码是通过Tomcat的JAAS控制管理的,接下来讲如何配置用户名和密码:

    在webapps/manager/WEB-INF/web.xml存放了manager应用的配置信息。如下:

                                                   

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 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.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">

  <display-name>Tomcat Manager Application</display-name>
  <description>
    A scriptable management web application for the Tomcat Web Server;
    Manager lets you view, load/unload/etc particular web applications.
  </description>

  <servlet>
    <servlet-name>Manager</servlet-name>
    <servlet-class>org.apache.catalina.manager.ManagerServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
  </servlet>
  <servlet>
    <servlet-name>HTMLManager</servlet-name>
    <servlet-class>org.apache.catalina.manager.HTMLManagerServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <!-- Uncomment this to show proxy sessions from the Backup manager or a
         StoreManager in the sessions list for an application
    <init-param>
      <param-name>showProxySessions</param-name>
      <param-value>true</param-value>
    </init-param>
    -->
    <multipart-config>
      <!-- 50MB max -->
      <max-file-size>52428800</max-file-size>
      <max-request-size>52428800</max-request-size>
      <file-size-threshold>0</file-size-threshold>
    </multipart-config>
  </servlet>
  <servlet>
    <servlet-name>Status</servlet-name>
    <servlet-class>org.apache.catalina.manager.StatusManagerServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>0</param-value>
    </init-param>
  </servlet>

  <servlet>
    <servlet-name>JMXProxy</servlet-name>
    <servlet-class>org.apache.catalina.manager.JMXProxyServlet</servlet-class>
  </servlet>

  <!-- Define the Manager Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>Manager</servlet-name>
      <url-pattern>/text/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Status</servlet-name>
    <url-pattern>/status/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>JMXProxy</servlet-name>
      <url-pattern>/jmxproxy/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>HTMLManager</servlet-name>
    <url-pattern>/html/*</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>SetCharacterEncoding</filter-name>
    <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>CSRF</filter-name>
    <filter-class>org.apache.catalina.filters.CsrfPreventionFilter</filter-class>
    <init-param>
      <param-name>entryPoints</param-name>
      <param-value>/html,/html/,/html/list,/index.jsp</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>CSRF</filter-name>
    <servlet-name>HTMLManager</servlet-name>
    <servlet-name>jsp</servlet-name>
  </filter-mapping>

  <!-- Define a Security Constraint on this Application -->
  <!-- NOTE:  None of these roles are present in the default users file -->
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>HTML Manager interface (for humans)</web-resource-name>
      <url-pattern>/html/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
       <role-name>manager-gui</role-name>
    </auth-constraint>
  </security-constraint>
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Text Manager interface (for scripts)</web-resource-name>
      <url-pattern>/text/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
       <role-name>manager-script</role-name>
    </auth-constraint>
  </security-constraint>
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>JMX Proxy interface</web-resource-name>
      <url-pattern>/jmxproxy/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
       <role-name>manager-jmx</role-name>
    </auth-constraint>
  </security-constraint>
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Status interface</web-resource-name>
      <url-pattern>/status/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
       <role-name>manager-gui</role-name>
       <role-name>manager-script</role-name>
       <role-name>manager-jmx</role-name>
       <role-name>manager-status</role-name>
    </auth-constraint>
  </security-constraint>

  <!-- Define the Login Configuration for this Application -->
  <login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>Tomcat Manager Application</realm-name>
  </login-config>

  <!-- Security roles referenced by this web application -->
  <security-role>
    <description>
      The role that is required to access the HTML Manager pages
    </description>
    <role-name>manager-gui</role-name>
  </security-role>
  <security-role>
    <description>
      The role that is required to access the text Manager pages
    </description>
    <role-name>manager-script</role-name>
  </security-role>
  <security-role>
    <description>
      The role that is required to access the HTML JMX Proxy
    </description>
    <role-name>manager-jmx</role-name>
  </security-role>
  <security-role>
    <description>
      The role that is required to access to the Manager Status pages
    </description>
    <role-name>manager-status</role-name>
  </security-role>

  <error-page>
    <error-code>401</error-code>
    <location>/WEB-INF/jsp/401.jsp</location>
  </error-page>
  <error-page>
    <error-code>403</error-code>
    <location>/WEB-INF/jsp/403.jsp</location>
  </error-page>
  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/404.jsp</location>
  </error-page>

</web-app>

通过上面的配置文件可以知道,登录Manager控制台需要不同的mananger角色,对于普通开发者而言,通常需要访问匹配test/*、/status/*的资源,因此需要为该用户配置一个manager-gui的角色即可。Tomcat用户的信息是在CONF目录下的tomcat-user.xml文件中配置。

 

<?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.
-->
<tomcat-users>
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.
-->
<!--
  NOTE:  The sample user and role entries below are wrapped in a comment
  and thus are ignored when reading this file. Do not forget to remove
  <!.. ..> that surrounds them.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
-->
</tomcat-users>

   我们进行如下配置添加:

<role rolename="manager-gui"/>
<user username="manager" password="manager" roles="manager-gui"/>

  然后重新启动服务器,点击manager apps按钮,输入用户名和密码,既可以进入控制台界面如下:

Tomcat的下载、安装、配置、管理_Tomcat_07      

部署Web应用

Tomcat中部署Web应用主要有如下几种方式:

利用Tomcat自动部署。

    该方式最简单,最常用。我们只要将一个Web应用复制到Tomcat的Webapps下面,系统会把该应用部署到Tomcat中。

利用控制台部署。

    该方式也很简单,根据控制台要求配置就可以了。

增加自定义的Web部署文件。

    这种方式不需要把Web应用复制到tomcat的安装目录下,只是部署方式有些麻烦,我们需要在conf目录下新建一个Catalina目录,再在Catalina目录下新建一个localhost目录,最后在目录下创建一个名字任意的XML文件,该文件适用于部署Web应用的配置文件,该主文件名将作为Web应用的虚拟路劲。

例如:在conf/Catalina/localhost下创建了一个dd.xml文件,文件如下:

<Context docBase="G:/publish/codes/01/aa" debug="0" privileged="true">
</Context>

修改Server.xml文件部署Web应用。

该种方式需要修改conf下的server.xml文件,修改可能会导致Tomcat系统破坏,一般不推荐使用该种方式。


配置Tomcat数据源

从Tomcat5.5开始Tomcat内置了DBCP的数据源实现,所以可以方便的配置DBCP数据源。Tomcat中有两种范围的数据源配置,一种是全局的数据源,一种是局部的(单个Web应用)数据源。

局部数据源配置就是在Web应用的配置文件中配置,不会造成系统紊乱,也可以防止其他的Web应用访问,提供了更好的封装性。

如上面的conf/Catalina/localhost下配置,dd.xml文件。

<?xml version="1.0" encoding="GBK"?>
<Context docBase="G:/publish/codes/01/aa" debug="0" privileged="true">
    <Resource name="jdbc/test" auth="Container"
                type="javax.sql.DataSource"
                driverClassName="com.mysql.jdbc.Driver"
                url="jdbc:mysql://localhost:3306/testdatabase"
                username = "root"
                password="123456"
                maxActive="10"
                maxIdle="2"
                maxWait="10000"/>
</Context>

    测试访问数据源的JSP页面:

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/testdatabase");
Connection conn = ds.getConnection();
Statement stmt = conn.creatStatement();
ResultSet rs = stmt.executeQuery("select * from user");
while(rs.next()){
out.println(rs.getString(1)+"\t"+rs.getString(2)+"</br>");
}
rs.close();
stmt.close();
conn.close();