天天看點

Tomcat的下載下傳、安裝、配置、管理

    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/ 按下Enter鍵出現如下頁面則表示TOmcat伺服器安裝成功。

配置Tomcat伺服器端口

Tomcat的服務端口預設是8080,可以通過修改配置檔案來改變服務端口,控制台等。Tomcat可以通過配置檔案同時多個端口提供服務。

通過修改server.xml檔案可以改變Tomcat的配置。

Tomcat的下載下傳、安裝、配置、管理
Tomcat的下載下傳、安裝、配置、管理

進入控制台

Tomcat有三個控制台:Server Status控制台:用于監控伺服器狀态。

Manager App控制台:用于部署、監控Weby應用。Host Manager控制台。

Tomcat的下載下傳、安裝、配置、管理

通常我們使用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的下載下傳、安裝、配置、管理

部署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應用通路,提供了更好的封裝性。

<?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>      
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();