标签:
在之前的章节都是通过Bootstrap类来初始化connector,context,wrapper。
而且手动来绑定它们的关系。
connector.setContainer(context);
或者手动设置实例的属性
context.setPath("/myApp") ;
context.setDocBase("myApp");
在tomcat中,是用web.xml来设置的。而digester是一个开源的库,可以读取xml,通过xml来加载类,实例化。
Digester
最普通的就是添加实例方法:
public void addObjectCreate(java.lang.String pattern, java.lang.Class clazz)
public void addObjectCreate(java.lang.String pattern, java.lang.String className)
设置属性:
public void addSetProperties(java.lang.String pattern),这是通过读取xml的内容,例:
digester.addObjectCreate("employee", "ex15.pyrmont.digestertest.Employee"); digester.addSetProperties("employee");
<employee firstName="Brian" lastName="May">
这个就会调用setFirstName和setLastName来设置属性
绑定对象的关系:
比如说Employee对象可以拥有多个Office。
digester.addObjectCreate("employee", "ex15.pyrmont.digestertest.Employee"); digester.addObjectCreate("employee/office", "ex15.pyrmont.digestertest.Office");
需要连续添加到digerter的栈里。
digester.addSetNext("employee/office", "addOffice");
Employee类
public class Employee { private String firstName; private String lastName; private ArrayList offices = new ArrayList(); public Employee () { System.out.println ("Creating Employee"); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { System.out.println("Setting firstName : " + firstName); this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { System.out.println("Setting lastName : " + lastName); this.lastName = lastName; } public void addOffice(Office office) { System.out.println("Adding Office to this employee"); offices.add(office); } public ArrayList getOffices() { return offices; } public void printName() { System.out.println("My name is " + firstName + " " + lastName); } }
<?xml version="1.0" encoding="ISO-8859-1"?> <employee firstName="Brian" lastName="May"> </employee>
public class Test01 { public static void main(String[] args) { String path = System.getProperty("user.dir") + File.separator + "etc"; File file = new File(path, "employee1.xml"); Digester digester = new Digester(); // add rules digester.addObjectCreate("employee", "ex15.pyrmont.digestertest.Employee"); digester.addSetProperties("employee"); digester.addCallMethod("employee", "printName"); try { Employee employee = (Employee) digester.parse(file); System.out.println("First name : " + employee.getFirstName()); System.out.println("Last name : " + employee.getLastName()); } catch(Exception e) { e.printStackTrace(); } } }
ContextConfig
在context启动前,需要绑定一个listener。这个listener设置了StandardContext实例。和之前的SimpleContexConfig只是简单地设置configured属性为true相比,ContextConfig做了很多准备工作。比如读取默认的web.xml和application的web.xml,来创建实例。将url和severlet相映射。
在ContextConfig中,lifecycleEvent(LifecycleEvent event)方法判断,如果是Lifecycle.START_EVENT这调用start()方法,如果是Lifecycle.STOP_EVENT则调用stop()方法。
其中,start()方法则调用defaultConfig(); applicationConfig(); 方法。分别读取默认的web.xml和application的web.xml。
新版的bootstrap,其中没有warpper的绑定。
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(); } } }
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>Modern</servlet-name> <servlet-class>ModernServlet</servlet-class> </servlet> <servlet> <servlet-name>Primitive</servlet-name> <servlet-class>PrimitiveServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Modern</servlet-name> <url-pattern>/Modern</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Primitive</servlet-name> <url-pattern>/Primitive</url-pattern> </servlet-mapping> </web-app>
《how tomcat work》 搬运工 Chapter 15: Digester
标签:
原文地址:http://www.cnblogs.com/xuyung/p/4927381.html