18,tomcat的主机(host)和引擎
如果需要在一个Tomcat部署中部署多个上下文,需要使用一个主机
引擎表示整个Catalina 的Servlet引擎。如果使用的话,它位于容器等级的最高层
可以添加到引擎上的容器包括org.apache.catalina.Host 或者org.apache.catalina.Context。
在一个Tomcat部署中,默认的容器是引擎。
Host相关的StandardHost、StandardHostMapper以及StandardHostValve类。
Host接口
主机是用org.apache.catalina.Host接口表示的。本接口继承了Container接口
public final class Bootstrap2 { public static void main(String[] args) {System.setProperty("catalina.base", System.getProperty("user.dir"));Connector connector = new HttpConnector(); Wrapper wrapper1 = new StandardWrapper(); wrapper1.setName("Primitive");wrapper1.setServletClass("PrimitiveServlet");Wrapper wrapper2 = new StandardWrapper();wrapper2.setName("Modern"); wrapper2.setServletClass("ModernServlet");Context context = new StandardContext();// StandardContext's start method adds a default mappercontext.setPath("/app1"); context.setDocBase("app1"); context.addChild(wrapper1); context.addChild(wrapper2);LifecycleListener listener = new SimpleContextConfig();((Lifecycle) context).addLifecycleListener(listener); Host host = new StandardHost(); host.addChild(context);host.setName("localhost");host.setAppBase("webapps");Loader loader = new WebappLoader();context.setLoader(loader); // context.addServletMapping(pattern, name); context.addServletMapping("/Primitive", "Primitive"); context.addServletMapping("/Modern", "Modern");Engine engine = new StandardEngine();engine.addChild(host); engine.setDefaultHost("localhost"); connector.setContainer(engine);try { connector.initialize(); ((Lifecycle) connector).start();((Lifecycle) engine).start(); // make the application wait until we press a key. System.in.read(); ((Lifecycle) engine).stop();}catch (Exception e) { e.printStackTrace(); }}}
?