Tomcat的一些实用配置

1-web应用和虚似目录的映射

Web应用开发好后,若想供外界访问,需要把web应用所在目录交给web服务器管理,这个过程称之为虚拟目录的映射。

Tomcat 6帮助文档(文档可以在C:\apache-tomcat-6.0.20\webapps\docs目录下的index.html中找到)中所提到的5种配置方式,这里只说第三种和第五种。
For Tomcat 6, unlike Tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. This is because it makes modifing the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat.

Context elements may be explicitly defined:

    1.In the $CATALINA_BASE/conf/context.xml file: the Context element information will be loaded by all webapps.

    2.In the $CATALINA_BASE/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all webapps of that host.

    3.In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. foo#bar.xml for a context path of /foo/bar. The default web application may be defined by using a file called ROOT.xml.

在C:\apache-tomcat-6.0.20\conf\Catalina\localhost目录下建文档a.xml,内容为:<Context docBase="c:\news" /> 这种方法服务器不需要重启  http://localhost:8080/a/1.html
 复制a.xml为b.xml http://localhost:8080/b/1.html
同样方式在这个目录下配置一个ROOT.xml文档,这个缺省的访问路径,配置完后要重启服务器,覆盖原来的缺省目录。

      4.Only if a context file does not exist for the application in the $CATALINA_BASE/conf/[enginename]/[hostname]/, in an individual file at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.

    5.Inside a Host element in the main conf/server.xml.

在C:\apache-tomcat-6.0.20\conf目录下的server.xml中配置如下:
<Context path="/itcast" docBase="c:\news"/>

但是这种配置不好,需要重启服务器。

一般在实际开发中使用第三种方式。

 

2.配置虚拟主机

在C:\apache-tomcat-6.0.20\conf目录下的server.xml中配置如下:

<Host name="www.sina.com" appBase="c:\sina">
 <Context path="/mail" docBase="c:\sina\mail"/>
</Host>

可以通过http://www.sina.com/mail/1,html来访问,但是我是在主机的主机上做的,所以这样做是不能访问的,浏览器会去找本地的hosts文件,发现没有这样的映射,然后去找DNS服务器进行域名解析。所以我们可以去hosts文件中进行配置。hosts文件位置:C:\Windows\System32\drivers\etc\hosts

 

3.配置网站主页

WEB-INF目录下的web.xml中输入如下代码:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <welcome-file-list>
        <welcome-file>1.html</welcome-file>        1.html就是主页,同时要在虚拟主机配置时path设置为缺省值""
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章