Tomcat

Tomcat

[email protected]

2015年7月9日

 

1 目标:作为web服务器,处理HTTP请求,共享数据,调用Web应用。

2 原理:实现Servlet规范。

Tomcat由Apache创建,遵守Servlet规范。

Tomcat接受HttpRequest,实例化业务Servlet,生成HttpResponse并返回。

2.1 组织结构:Server作为整个Tomcat的代表,包含多个服务(Service)。每个服务由servlet处理引挚(Engine),监听接口(Connector)和应用(Host,提供具体服务)组成。

Tomcat自身:server。配置整个环境的参数。

服务:Service,表示提供的服务类别。

servlet处理器:处理servlet的实现。

监听接口:connector,监听客户端请求。

应用:响应请求的实现。

2.2 示例:tomcat的server.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 informationregarding copyright ownership.

  The ASF licenses this file to You under theApache License, Version 2.0

  (the "License"); you may not usethis 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 agreedto in writing, software

  distributed under the License is distributedon an "AS IS" BASIS,

  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.

  See the License for the specific languagegoverning 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

 -->

<Serverport="8005" shutdown="SHUTDOWN">

  <!-- Security listener. Documentation at/docs/config/listeners.html

  <ListenerclassName="org.apache.catalina.security.SecurityListener" />

  -->

  <!--APR library loader. Documentation at/docs/apr.html -->

  <ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on" />

  <!--Initialize Jasper prior to webapps areloaded. Documentation at /docs/jasper-howto.html -->

  <ListenerclassName="org.apache.catalina.core.JasperListener" />

  <!-- Prevent memory leaks due to use ofparticular java/javax APIs-->

  <ListenerclassName="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>

  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>

  <ListenerclassName="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 authenticateusers

    -->

    <Resource name="UserDatabase"auth="Container"

             type="org.apache.catalina.UserDatabase"

              description="User databasethat can be updated and saved"

             factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

             pathname="conf/tomcat-users.xml" />

  </GlobalNamingResources>

 

  <!-- A "Service" is a collectionof one or more "Connectors" that share

       a single "Container"Note:  A "Service" is notitself a "Container",

       so you may not define subcomponents suchas "Valves" at this level.

       Documentation at/docs/config/service.html

   -->

  <Service name="Catalina">

 

    <!--The connectors can use a sharedexecutor, you can define one or more named thread pools-->

    <!--

    <Executorname="tomcatThreadPool" namePrefix="catalina-exec-"

        maxThreads="150"minSpareThreads="4"/>

    -->

 

 

    <!-- A "Connector" representsan endpoint by which requests are received

         and responses are returned.Documentation at :

         Java HTTP Connector:/docs/config/http.html (blocking & non-blocking)

         Java AJP  Connector: /docs/config/ajp.html

         APR (HTTP/AJP) Connector:/docs/apr.html

         Define a non-SSL HTTP/1.1 Connector onport 8080

    -->

    <Connector port="8080"protocol="HTTP/1.1"

              connectionTimeout="20000"

               redirectPort="8443"/>

    <!-- A "Connector" using theshared thread pool-->

    <!--

    <Connectorexecutor="tomcatThreadPool"

               port="8080"protocol="HTTP/1.1"

              connectionTimeout="20000"

               redirectPort="8443"/>

    -->

    <!-- Define a SSL HTTP/1.1 Connector onport 8443

         This connector uses the JSSEconfiguration, when using APR, the

         connector should be using the OpenSSLstyle configuration

         described in the APR documentation-->

    <!--

    <Connector port="8443"protocol="HTTP/1.1" SSLEnabled="true"

               maxThreads="150"scheme="https" secure="true"

               clientAuth="false"sslProtocol="TLS" />

    -->

 

    <!-- Define an AJP 1.3 Connector on port8009 -->

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

 

 

    <!-- An Engine represents the entrypoint (within Catalina) that processes

         every request.  The Engine implementation for Tomcat standalone

         analyzes the HTTP headers includedwith the request, and passes them

         on to the appropriate Host (virtualhost).

         Documentation at/docs/config/engine.html -->

 

    <!-- You should set jvmRoute to supportload-balancing via AJP ie :

    <Engine name="Catalina"defaultHost="localhost" jvmRoute="jvm1">

    -->

    <Engine name="Catalina"defaultHost="localhost">

 

      <!--For clustering, please take a lookat documentation at:

          /docs/cluster-howto.html  (simple how to)

          /docs/config/cluster.html (referencedocumentation) -->

      <!--

      <ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

      -->

 

      <!-- Use the LockOutRealm to preventattempts to guess user passwords

           via a brute-force attack -->

      <RealmclassName="org.apache.catalina.realm.LockOutRealm">

        <!-- This Realm uses theUserDatabase configured in the global JNDI

             resources under the key"UserDatabase".  Any edits

             that are performed against thisUserDatabase are immediately

             available for use by theRealm.  -->

        <RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"

              resourceName="UserDatabase"/>

      </Realm>

 

      <Host name="localhost"  appBase="webapps"

            unpackWARs="true"autoDeploy="true">

 

        <!-- SingleSignOn valve, shareauthentication between web applications

             Documentation at:/docs/config/valve.html -->

        <!--

        <ValveclassName="org.apache.catalina.authenticator.SingleSignOn" />

        -->

 

        <!-- Access log processes allexample.

             Documentation at:/docs/config/valve.html

             Note: The pattern used isequivalent to using pattern="common" -->

        <ValveclassName="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>

 

3 安装:JRE_HOME,JDK下的jre路径。JAVA_HOME:JDK路径。

参考:http://modiliany.iteye.com/blog/1503460

http://blog.csdn.net/nero_2012/article/details/7550436

3.1 Linux配置方法:参见..\CPP\Linux\Linux.docx Tomcat部分。

3.2 程序结构

运行程序目录:bin

配置文件目录:conf

依赖库目录:lib

系统日志:log,如果web应用中有问题,可能到这里查找日志。

临时目录:temp

应用程序目录:webapps

JVM工作目录:work。

其中bin和lib为所有tomcat实例公用,其它的则为每个实例都有一个独立的备份。

catalina.home(安装目录):指向公用信息的位置,就是bin和lib的父目录。

catalina.base(工作目录,默认为catalina.home):指向每个Tomcat目录私有信息的位置,就是conf、logs、temp、webapps和work的父目录。

如下所示为eclipse默认的tomcat实例结构。

参考:http://blog.csdn.net/xiaohai0504/article/details/7631014

3.3 配置Java SDK目录:JAVA_HOME

tomcat依赖sdk中的jar包。

 

4 启动/关闭:startup.bat,shutdown.bat。

bin/startup.bat启动tomcat。

启动后可以使用web页面进行管理。

5 配置:localhost:8080是tomcat的配置页面。在tomcat/conf下是所有的配置文件。

配置方法详见:..\CPP\Linux\Linux.docxTomcat部分。

5.1 配置文件方式配置

服务器:Server.xml。

服务程序:Server.xml的Service标签。复制可实现多个服务。

服务端口:Server.xml的Connector标签管理服务端口。默认是8080。

用户角色:webapps/manager/web-inf/web.xml的security-role中有角色列表。

用户管理:tomcat-users.xml。

5.2 页面方式配置

服务器状态:ServerStatus。

应用管理:Manager App。使用用户角色在用户角色配置文件中生成用户后,在页面中登录配置。

6 部署web服务:webapps目录

直接copy或使用页面配置(后台自动copy)。

虚拟目录:Catalina/localhost/配置文件.xml。在配置文件中Context标签映射物理路径。

格式:<Context docBase=”realDir” debug=”0” privileged=”true”></Context>

7 数据源:数据库连接池(DBCP)

全局:修改Server.xml文件。所有服务可用,慎用。

局部:修改局部web部署文件。增加Resource标签。

8 调试:日志

8.1 windows:控制台输出、logs日志

8.1.1控制台:从控制台看到输出的错误内容,然后查找服务器启动的消息,定位错误。

8.1.2日志:控制台内容、服务器(java)消息

控制台输出内容:catalina.日期.log。

服务器消息内容:localhost.日期.log。

参考:http://modiliany.iteye.com/blog/1503460

http://blog.csdn.net/nero_2012/article/details/7550436

8.2 Linux:只能使用日志调试

8.2.1查看控制台输出内容:logs/catalina.out

8.2.2查看服务器消息:localhost.日期.log。

参考:http://blog.csdn.net/huangbiao86/article/details/6645512

你可能感兴趣的:(java,tomcat,javaee,servlet,web服务器)