How Tomcat work Porter Chapter 15:digester

Source: Internet
Author: User

The previous chapters were initialized with the bootstrap class to initialize the Connector,context,wrapper.

And to bind their relationships manually.

Connector.setcontainer (context);

Or manually set the properties of the instance

Context.setpath ("/myapp");

Context.setdocbase ("myApp");

In Tomcat, it is set with Web. Xml. And Digester is an open source library that can read XML, load classes through XML, instantiate.

Digester

The most common is to add an instance method:

public void Addobjectcreate (java.lang.String pattern, Java.lang.Class clazz)
public void Addobjectcreate (java.lang.String pattern, java.lang.String className)

To set properties:

public void Addsetproperties (java.lang.String pattern), which is by reading the contents of XML, for example:

Digester.addobjectcreate ("Employee", "Ex15.pyrmont.digestertest.Employee"); Digester.addsetproperties ("employee");

<employee firstname= "Brian" lastname= "may" >

This will call Setfirstname and Setlastname to set the property

The relationship of the bound object:

For example, an employee object can have more than one office.

Digester.addobjectcreate ("Employee", "Ex15.pyrmont.digestertest.Employee"); Digester.addobjectcreate ("Employee/office", "Ex15.pyrmont.digestertest.Office");

Need to be added to the Digerter stack continuously.

Digester.addsetnext ("Employee/office", "Addoffice");

Employee class

 Public classEmployee {PrivateString FirstName; PrivateString LastName; PrivateArrayList offices =NewArrayList ();  PublicEmployee () {System.out.println ("Creating Employee"); }          PublicString Getfirstname () {returnFirstName; }          Public voidsetfirstname (String firstName) {System.out.println ("Setting firstName:" +firstName);  This. FirstName =FirstName; }          PublicString Getlastname () {returnLastName; }          Public voidsetlastname (String lastName) {System.out.println ("Setting lastName:" +lastName);  This. LastName =LastName; }          Public voidaddoffice (Office Office) {SYSTEM.OUT.PRINTLN ("Adding Office to this employee");    Offices.add (Office); }        PublicArrayList getoffices () {returnoffices; }        Public voidPrintname () {System.out.println ("My name is" + FirstName + "" +lastName); } } 
<? XML version= "1.0" encoding= "Iso-8859-1" ?> <  firstName= "Brian"  lastName= "May"></  employee>
 Public classTest01 { Public Static voidMain (string[] args) {String path= System.getproperty ("User.dir") + File.separator + "etc"; File File=NewFile (Path, "Employee1.xml"); Digester digester=Newdigester (); //Add RulesDigester.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

Before the context is started, a listener needs to be bound. This listener sets the Standardcontext instance. And before the simplecontexconfig just simply set the configured property to True, Contextconfig did a lot of prep work. For example, read the default Web. XML and application Web. XML to create an instance. The URL and Severlet are mapped.

In Contextconfig, the Lifecycleevent (Lifecycleevent event) method determines if lifecycle.start_event this calls the START () method, if it is lifecycle.stop _event calls the Stop () method.

where the start () method calls Defaultconfig (); Applicationconfig (); Method. Read Web. XML for the Default Web. XML and application, respectively.

The new version of Bootstrap, where there is no warpper binding.

 Public Final classBootstrap {//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 voidMain (string[] args) {System.setproperty ("Catalina.base", System.getproperty ("User.dir")); Connector Connector=NewHttpConnector (); Context Context=NewStandardcontext (); //Standardcontext ' s Start method adds a default mapperContext.setpath ("/app1"); Context.setdocbase ("App1"); Lifecyclelistener Listener=NewContextconfig ();    ((Lifecycle) context). Addlifecyclelistener (listener); Host host=NewStandardhost ();    Host.addchild (context); Host.setname ("LocalHost"); Host.setappbase ("WebApps"); Loader Loader=NewWebapploader ();    Context.setloader (loader);    Connector.setcontainer (host); Try{connector.initialize ();      ((Lifecycle) connector). Start ();      ((Lifecycle) host). Start (); Container[] C=Context.findchildren (); intLength =c.length;  for(inti=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 Porter Chapter 15:digester

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.