tomcat context配置


The Context element represents a web application, which is run within a particular virtual host. Each webapplication is based on a Web Application Archive (WAR) file, or a corresponding directory containing thecorresponding unpacked contents, as described in the Servlet Specification(version 2.2 or later). For more information about web application archives,you can download the Servlet Specification,and review the Tomcat Application Developer's Guide.

Theweb application used to process each HTTP request is selected by Catalina basedon matching the longest possible prefix[c1] of the Request URI against the context path of each defined Context.Once selected, that Context will select an appropriate servlet to process theincoming request, according to the servlet mappings defined by the webapplication deployment.

Youmay define as many Context elementsas you wish. Each such Context MUST have a unique context name within a virtualhost. The context path does not need to be unique (see paralleldeployment below). In addition, a Contextmust be present with a context path equal to a zero-length string. This Contextbecomes the default webapplication for this virtual host, and is used to process all requests that donot match any other Context's context path.

 

context的参数:

crossContext="true" ,是允许应用通过ServletContext.getContext() 去拿到一个通往别的应用request dispatcher 。当然了,这种方法无法跨越现在Tomcat 支持的虚拟主机界限。也就是说,能够穿透访问的,必须是和当前应用在一个<Host> 之中的应用。

privileged="true" 意味着 Tomcat自身的应用,比如·Tomcat Manager ,可以被当前这个应用访问。根据官方文档的解释,这个机理是改变应用的类加载器为Server class loader 。我想,这种改变,会令应用程序发现Tomcat 本身的类,都能够从应用自己的类加载器上寻找到。从而实现对Tomcat 自身应用程序方法的调用。

path docBase 不用多说,都要指定这二个属性的。其中docBase 可以是目录也可以是结构完整的.war 文件。

reloadable="true" 意味着 Tomcat将提供对应用类路径(/WEB-INF/classes/ /WEB-INF/lib/ )的监测。当这里边有内容改变并且其类已经被爪哇虚拟机(JVM)加载的时候,Tomcat 可以自行重新加载此类。不过此功能对Tomcat 的稳定服务影响不小,调试环境可以使用,生产环境还是算了吧——当然,这只是我的个人建议。

unpackWAR 就如字面意思,unpackWAR="true"意味着 Tomcat会保存 .war 包的解压结果,然后直接对解压结果进行运行。我个人认为,考虑到爪哇虚拟机的类加载机制,每个类都仅加载一回,但是页面内容却没有类似的有效缓存,所以.war 还是解压执行的比较好。而且日志也将造成unpackWAR="false" 形同灾难。

cachingAllowed="true" 意味着开启了Tomcat7 的静态缓存功能。静态文件包括JavaScript 程序、图片声音等允许网络访问的文件以及HTML 页面。

cacheMaxSize 是静态缓存功能缓冲区大小的设定。单位是MB ,也就是 1024KB。例子中设为 1024 ,意味着 1GB


### Tomcat Context 配置与使用 #### 什么是Context? `<Context>` 是Tomcat中的一个重要组件,它代表了一个Web应用程序的上下文环境。每个部署的应用程序都有自己的`<Context>`实例,用于定义该应用的各种属性和资源路径。 #### `<Context>` 的位置 通常情况下,可以找到多个地方来设置`<Context>`: - **全局配置**:位于 `$CATALINA_HOME/conf/context.xml` 文件内,这里的配置适用于所有已部署的应用。 - **特定应用配置**:对于单个webapp来说,可以在其 `META-INF/context.xml` 中指定特殊参数;也可以通过在服务器端conf目录下的相应子文件夹里放置名为context.xml的文档来进行个性化设定[^1]。 #### 主要配置项解释 以下是几个重要的配置选项及其作用说明: | 属性名 | 描述 | | --- | --- | | path | 定义访问此WebApp时使用的URL前缀,默认为空字符串(`""`)表示根路径。| | docBase | 应用的实际物理存储位置,即WAR包解压后的目录或是源码所在的位置。| | reloadable | 设置为true则允许热加载功能开启,当检测到类变化会自动重新加载整个application (开发阶段推荐启用) 。| 示例代码展示如何自定义一个简单的`<Context>`片段: ```xml <!-- Example of a minimalistic custom context --> <Context> <!-- Specifies the URL pattern used to access this application --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- Enables automatic reloading when classes change during development --> <Resources cachingAllowed="false"/> <!-- Other configurations... --> </Context> ``` 为了使更改生效并验证是否正确设置了`<Context>`,记得按照指示重启Tomcat服务[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值