How Tomcat Work 之Digester之The Applicaition



这一章节的应用允许你使用一个ContextConfig作为监听器来配置StandardContext对象。其由一个类,BootStrap组成。代码清单如下:



package ex15.pyrmont.startup;

import org.apache.catalina.Connector;
import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Host;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Loader;
import org.apache.catalina.connector.http.HttpConnector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.ContextConfig;

public final class Bootstrap {
    
 
    // invoke: http://localhost:8080/app1/Modern or
   // http://localhost:8080/app2/Primitive
   // note that we don't instantiate a Wrapper here,
   // ContextConfig reads the WEB-INF/classes dir and loads all servlets.
   public static void main(String[] args) {
       System.setProperty("catalina.base",System.getProperty("user.dir"));
       Connector connector = new HttpConnector();
       Context context = new StandardContext();
       // StandardContext's start method adds a default mapper
       context.setPath("/app1");
       context.setDocBase("app1");
       LifecycleListener listener = new ContextConfig();
       ((Lifecycle) context).addLifecycleListener(listener);
       Host host = new StandardHost();
       host.addChild(context);
       host.setName("localhost");
       host.setAppBase("webapps");

       
       Loader loader = new WebappLoader();
       context.setLoader(loader);
       connector.setContainer(host);
       try {
           connector.initialize();
           ((Lifecycle) connector).start();
           ((Lifecycle) host).start();
           Container[] c = context.findChildren();
           int length = c.length;
           for (int i=0; i<length; i++) {
                Container child = c[i];
                System.out.println(child.getName());
           }

           // make the application wait until we press a key.
          System.in.read();
          ((Lifecycle) host) .stop();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

   


总结


Tomcat可以有不同的配置。使用一个server.xml文件的简单配置可以通过使用Digester对象将XML元素转为Java对象完成。另外,一个web.xml文档用于配置一个servlet/jsp的应用程序。Tomcat必须能够解析这个web.xml文档并且基于XML文档中的元素配置一个Context对象。Digester再一次优美的解决了这个问题。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值