zoukankan      html  css  js  c++  java
  • tomcat j2ee 目录结构

    一、TOMCAT的目录结构

    /bin:存放windows或Linux平台上启动和关闭Tomcat的脚本文件

    /conf:存放Tomcat服务器的各种全局配置文件,其中最重要的是server.xml和web.xml

    /doc:存放Tomcat文档

    /server:包含三个子目录:classes、lib和webapps

    /server/lib:存放Tomcat服务器所需的各种JAR文件

    /server/webapps:存放Tomcat自带的两个WEB应用admin应用和 manager应用

    /common/lib:存放Tomcat服务器以及所有web应用都可以访问的jar文件

    /shared/lib:存放所有web应用都可以访问的jar文件(但是不能被Tomcat服务器访问)

    /logs:存放Tomcat执行时的日志文件

    /src:存放Tomcat的源代码

    /webapps:Tomcat的主要Web发布目录,默认情况下把Web应用文件放于此目录

    /work:存放JSP编译后产生的class文件

    Tomcat的配置文件

    Tomcat的配置基于两个配置文件:

    1.server.xml - Tomcat的全局配置文件               2.web.xml - 在Tomcat中配置不同的关系环境

     

    server.xml

    server.xml是Tomcat的主配置文件.完成两个目标:

    1 提供Tomcat组件的初始配置.

    2 说明Tomcat的结构,含义,使得Tomcat通过实例化组件完成起动及构建自身, 如在server.xml所指定的

     

    server.xml种的重要元素:

    元素及其描述

    Server

    server.xml文件中最重要的元素.Server定义了一个Tomcat服务器.一般你不用对他担心太多.Server元素能包含Logger和ContextManager元素类型

    Logger

    此元素定义一个Logger对象,每个Logger都有一个名字去标识,也有一个纪录Logger的输出和冗余级别(描述此日志级别)和包含日志文件的路径.通常有servlet的Logger(ServletContext.log()处),JSP和Tomcat运行时的Logger.

    ContextManager

    ContextManager说明一套ContextInterceptor, RequestInterceptor , Context和他们的Connectors的配置及结构.ContextManager有几个随同提供的特性:

    1. 用来纪录调试信息的调试级别

    2. webapps/,conf/,logs/和所有已定义的环境的基本位置.用来使Tomcat可以在TOMCAT_HOME外的其他目录启动.

    3. 工作目录的名字

    ContextInterceptor&RequestInterceptor

    这些侦听器(interceptors)侦听具体发生在ContextManager中的事件.例如,ContextInterceptor侦听Tomcat的启动及终止事件,RequestInterceptor监视在它服务过程中用户请求需要通过的不同阶段.Tomcat的管理员不必知道太多关于侦听器的知识;另外,开发者应该知道这是如何在Tomcat中实现一个”全局”型的操作(例如安全性及每个请求日志)

    Connector

    Connector表示一个到用户的联接,不管是通过web服务器或直接到用户浏览器(在一个独立配置中).Connector负责管理Tomcat的工作线程和 读/写 连接到不同用户的端口的 请求/响应.Connector的配置包含如下信息:

    1.句柄类

    2.句柄监听的TCP/IP端口

    3.句柄服务器端口的TCP/IP的backlog.

    Context

    每个Context提供一个指向你放置你Web项目的Tomcat的下属目录。每个Context包含如下配置:

    1. Context放置的路径,可以是与ContextManager主目录相关的路径.

    2.纪录调试信息的调试级别

    3.可重载的标志.开发Servlet时,重载更改后的Servlet,这是一个非常便利的特性,你可以调试或用Tomcat测试新代码而不用停止或重新启动Tomcat.要打开重载,把reloadable设为真即可.这虽花费时间但可检测所发生的变化;更重要的事,鉴于,在一个装载类对象装入一个新的servlet时,类装载触发器可能会掷出一些错误.为避免这些问题,你可以设置可重载为假,这将停止重载功能.

     

    web.xml

         Tomcat可以让用户通过将缺省的web.xml放入conf目录中来定义所有关系环境的web.xml的缺省值.建立一个新的关系环境时,Tomcat使用缺省的web.xml文件作为基本设置和应用项目特定的web.xml(放在应用项目的WEB-INF/web.xml文件)来覆盖这些缺省值.

     

    二、WEB应用的目录结构:假设在$CATALINA_HOME/webapps下有helloapp的web应用

    Web应用的目录结构

    目  录

    描  述

    /helloapp

    Web应用的根目录,所有的JSP和HTML文件都存放于此目录

    /helloapp/WEB-INF

    存放Web应用的发布描述文件web.xml

    /helloapp/WEB-INF/classes

    存放各种class文件,Servlet类文件也放于此目录下

    /helloapp/WEB-INF/lib

    存放Web应用所需的各种JAR文件。例如,在这个目录下,可以存放JDBC驱动程序的JAR文件。

    注:在classes以及lib子目录下,都可以存放Java类文件。在运行过程中,Tomcat的类装载器先装载classes目录下的类,再装载lib.

    注意:凡是WEB-INF里面的文件都不能被客户端直接访问(比如隐藏的信息)。WEB-INF目录下的资源对用户来说是不可见的,而对Web服务器来说则没有这样的限制

    例如在WEB-INF下的index.htm,客户端无法与对待其他文件夹内的信息一样,通过http://yourserver/yourwebapp/WEB-INF/index.htm访问。WEB-INF文件夹是禁止通过URL访问的。

     
     

    三、Tomcat加载类和资源的顺序为(以helloapp应用为例):

    1、helloapp (/webapps/helloapp/Web-INF/下的classes,lib子目录中*.calss及*.jar,仅helloapp可以加载)

    2、Bootstrap ($JAVA_HOME/jre/lib/ext/*.jar)

    3、System ($CLASSPATH/*.class和CLASSPATH中指定的jar)

    4、Common ($CATALINA_HOME/common/下的classes,lib,endores子目录中*.class及*.jar)

    5、Catalina ($CATALINA_HOME/server/下的classes,lib子目录中*.calss及*.jar,仅Tomcat可以加载)

    6、Shared ($CATALINA_HOME/shared/下的classes,lib子目录中*.calss及*.jar,仅所有Web应用可以加载)

    四、$CATALINA_HOME/conf/server.xml配置简介

    1、元素属性

    port:指定一个端口,这个端口负责监听关闭tomcat的请求

    shutdown:指定向端口发送的命令字符串,通常是“SHUTDOWN”

    2、元素属性

    name:指定service的名字,通常是”Catalina”

    3、元素属性(表示客户端和service之间的连接):

    port:指定服务器端要创建的端口号,并使用这个断口监听来自客户端的请求

    minProcessors(考察是否有此属性):服务器端启动时创建的处理请求的线程数

    maxProcessors(考察是否有此属性):最大可以创建的处理请求的线程数

    maxThreads:服务器创建的最大线程数

    minSpareThreads:最小剩余线程数

    maxSpareThreads:最大剩余线程数

    enableLookups:如果为true,则可以通过调用request.getRemoteHost()进行DNS查询来得到远程客户端的实际主机名,若为false则不进行DNS查询,而是返回其IP地址

    redirectPort:指定当服务器正在处理HTTP请求时收到了一个SSL传输请求后重定向的端口号

    acceptCount:指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过的请求将不予处理

    connectionTimeout:指定超时的时间数(以毫秒为单位)

    disableUploadTimeout:不明

    debug:不明

    protocol:不明

    4、元素属性(指定service中的请求处理主机,接收和处理来自Connector的请求)

    name:指定Engine的名字,通常是”Catalina”

    defaultHost:指定默认的处理请求的主机名,至少与某个host元素的name属性值是相同的

    5、元素属性(表示一个web应用程序)

    docBase:指定此Web应用的绝对或相对路径,也可以为WAR文件的路径

    path:表示此Web应用程序的url的入口,如为“/hello“,则请求的URL为http://localhost:8080/hello/

    reloadable:如果为true,则Tomcat在运行时会自动监视Web应用的/WEB-INF/lib和/WEB-INF/classes下文件的改动,自动装载新应用,使我们可以在不重启Tomcat的情况下更新Web应用

    6、元素属性(表示一个虚拟主机)

    name:指定虚拟主机名字

    debug:指定日志级别

    appBase:存放Web应用程序的基本目录,可以是绝对路径或相对于$CATALINA_HOME的目录,默认是$CATALINA_HOME/webapps

    unpackWARs:如果为true,则tomcat会自动将WAR文件解压后运行,否则不解压而直接从WAR文件中运行应用程序

    autoDeploy:如果为true,表示Tomcat启动时会自动发布appBase目录下所有的Web应用(包括新加入的Web应用)

    deployOnStarup:如果此项为true,表示Tomcat服务器启动时会自动发布appBase目录下所有Web应用。如果Web应用在 server.xml中没有相应的元素,则将采用默认的Context配置。deployOnStarup的默认设置是 true

    alias:指定虚拟主机的别名,可以指定多个别名

    xmlValidation:不明

    xmlNamespaceAware:不明

    7、元素属性(表示日志,调试和错误信息)

    className:指定logger使用的类名,此类必须实现org.apache.catalina.Logger 接口

    prefix:指定log文件名的前缀(文件名)

    suffix:指定log文件名的后缀(扩展名)

    timestamp:如果为true,则log文件名中会加入日期时间,如下例:localhost_log.2006-10-04.txt

    directory:指定log文件存放的目录

    8、元素属性(表示存放用户名,密码及role的数据库)

    className:指定Realm使用的类名,此类必须实现org.apache.catalina.Realm接口

    resourceName:不明

    9、元素属性(功能与Logger相似,其prefix和suffix属性解释和Logger 中的一样)

    className:指定Valve使用的类名,如用org.apache.catalina.valves.AccessLogValve类可以记录应用程序的访问信息

    directory:指定log文件存放的位置

    pattern:有两个值,common方式记录远程主机名或ip地址、用户名、日期、第一行请求的字符串、HTTP响应代码、发送的字节数。combined方式比common方式记录的值还多

    Java web组成
    Java web应用由一组静态HTML页、ServletJSP和其他相关的class组成。每种组件在web应用中都有固定的存放目录。web应用的配置信息存放在web.xml文件中。在发布某些组件(如Servlet)时,必须在web.xml文件中添加相应的配置信息

    web应用的目录结构
    web应用具有固定的目录结构,比如开发一个名为helloapp的应用,在<CATALINA_HOME>/webapps目录下创建这个目录结构。
    /helloapp web应用的根目录,所有的JSP和HTML文件都存放于此目录下
    /helloapp/WEB-INF 存放web应用的发布描述文件web.xml
    /helloapp/WEB-INF/classes 存放各种class文件,Servlet类文件也放于此目录
    /helloapp/WEB-INF/lib 存放web应用所需的各种JAR文件,比如可以存放JDBC驱动程序的JAR文件
    tomcat的类装载器先装载classes目录下的类,再装载lib目录下的类,如果两个目录下存在同名的类,classes目录下的类具有优先权

    在server.xml中加入<Context>元素
    <Context>元素是<CATALINA_HOME>/conf/server.xml中使用最频繁的元素,它代表了运行在<Host>上的单个Web应用,一个<Host>中可以有多个<Context>元素,每个Web应用必须有唯一的URL路径,path属性设定。
    <!--Define the default virtual host-->
    <Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true">
    ....
    ....
    <Context path="/helloapp" docBase="helloapp" debug="0" reloadable="true" />
    </Host>

    path属性:指定访问该web应用的URL入口
    docBase:指定web应用的文件路径,可以给绝对路径,也可以给Host的appBase的相对路径
    reloadable:如果设置为true,tomcat服务器在运行状态下会监视在WEB-INF/classes和lib目录下class文件的改动,如果被更新,服务器会自动重新加载web应用

    也可以放在你想放的任何位置的。 
    在server.xml中, 
    <Host ....> 
    <!--加上如下这句话--> 
    <Context path="/myweb" docBase="c:/jsp" debug="0" 
    reloadable="true" crossContext="true" /> 

    </Host> 
    然后你的文件全部放在c:jsp下,用 http://IP:port/myweb/index.jsp 来访问

    创建并发布WAR文件
    1>进入helloapp应用的根目录<CATALINA_HOME>/webapps目录下
    2>把整个Web应用打包为helloapp.war,命令为: jar cvf helloapp.war *.*(解压参数为xvf)
    3>把helloapp.war文件复制到<CATALINA_HOME>/webapps目录下
    4>删除原来的helloapp整个目录(一定要删除整个目录)
    5>启动Tomcat服务器
    根据<CATALINA_HOME>/conf/server.xml里面Host的unpackWARs属性,如果为true,则Tomcat启动的时候会自动把war文件解压,展开为开放式的目录结构。

    标准web应用的组成结构:

    To facilitate creation of a Web Application Archive file in the required format, it is convenient to arrange the "executable" files of your web application (that is, the files that Tomcat actually uses when executing your app) in the same organization as required by the WAR format itself. To do this, you will end up with the following contents in your application's "document root" directory:

    • *.html, *.jsp, etc. - The HTML and JSP pages, along with other files that must be visible to the client browser (such as JavaScript, stylesheet files, and images) for your application. In larger applications you may choose to divide these files into a subdirectory hierarchy, but for smaller apps, it is generally much simpler to maintain only a single directory for these files. 

    • /WEB-INF/web.xml - The Web Application Deployment Descriptor for your application. This is an XML file describing the servlets and other components that make up your application, along with any initialization parameters and container-managed security constraints that you want the server to enforce for you. This file is discussed in more detail in the following subsection. 

    • /WEB-INF/classes/ - This directory contains any Java class files (and associated resources) required for your application, including both servlet and non-servlet classes, that are not combined into JAR files. If your classes are organized into Java packages, you must reflect this in the directory hierarchy under /WEB-INF/classes/. For example, a Java class named com.mycompany.mypackage.MyServlet would need to be stored in a file named /WEB-INF/classes/com/mycompany/mypackage/MyServlet.class

    • /WEB-INF/lib/ - This directory contains JAR files that contain Java class files (and associated resources) required for your application, such as third party class libraries or JDBC drivers.

    When you install an application into Tomcat (or any other 2.2 or later Servlet container), the classes in the WEB-INF/classes/ directory, as well as all classes in JAR files found in the WEB-INF/lib/ directory, are made visible to other classes within your particular web application. Thus, if you include all of the required library classes in one of these places (be sure to check licenses for redistribution rights for any third party libraries you utilize), you will simplify the installation of your web application -- no adjustment to the system class path (or installation of global library files in your server) will be necessary.

    Much of this information was extracted from Chapter 9 of the Servlet API Specification, version 2.3, which you should consult for more details.

    转自:http://tomcat.apache.org/tomcat-7.0-doc/appdev/deployment.html

  • 相关阅读:
    GOF23设计模式汇总
    获取表单提交MVC错误信息
    Spring.Net
    简单工厂、工厂方法和抽象工厂
    Json和JsonP
    mysql8无法用navicat连接(mysql8加密方式的坑)
    (4.16)mysql备份还原——物理备份之XtraBackup实践
    mysql如何下载历史版本?
    如何测试端口通不通(四种方法)
    linux移动复制删除命令
  • 原文地址:https://www.cnblogs.com/youxin/p/3469805.html
Copyright © 2011-2022 走看看