- 浏览: 467457 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (371)
- Java (44)
- Flex (25)
- Design Pattern / UML (5)
- JVM (14)
- Software Engineer (4)
- Testing (30)
- Web Backend (60)
- Linux (7)
- Database (11)
- SOA (28)
- English (6)
- FX (5)
- SAP Support (25)
- SAP Development (5)
- SAP Impl & Oprn (8)
- Articles (15)
- Music (4)
- Recipe (14)
- Multi-thread (4)
- Lucene (2)
- CS basic / Algorithm (12)
- Tour (3)
- C C++ (21)
- Others (10)
- Code Repository C++ (1)
- Code Repository Java (1)
- Code Repository Algorithm (1)
- Code Repository SQL (1)
- Web Frontend (4)
最新评论
-
xiangxm:
Java 强引用、 软引用、 弱引用、虚引用 -
nml-lyb:
123
Mule 的第一个例子 -
nml-lyb:
http://www.baidu.com
Mule 的第一个例子 -
white___ant:
...
Java 强引用、 软引用、 弱引用、虚引用 -
joeyon:
...
Java NIO介绍
Chapter 15 : Digester
概览
在之前的章节中,我们用Bootstrap类来实例化connector, context, wrappers 以及其他的组件。当把组件注入到父级组件中时,我们使用的是set方法,比如说:
connector.setContainer(context);
context.setPath("/myApp");
context.setDocBase("myApp");
再比如说:
context.setLoader(loader);
context.addChild(wrapper1);
context.addChild(wrapper2);
这种注入方式的缺点是不灵活,改动时需要动源代码。Tomcat4使用了更加灵活的方式,即使用xml配置文件的方法。
Digester
Digester是一个开源的项目,解析xml文件,然后把里面的element变为java object。
下面给出三个小例子,很容易了解digester的使用方法\
Example 1 :
employee1.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<employee firstName="Brian" lastName="May">
</employee>
Employee.java
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);
}
}
Test01.java
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();
}
}
}
结果:
Creating Employee Setting firstName : Brian Setting lastName : May My name is Brian May First name : Brian Last name : May
Example 2 :
xml file :
<?xml version="1.0" encoding="ISO-8859-1"?> Office.java public class Office { Address.java Test02.java public class Test02 { public static void main(String[] args) { 运行结果为:
Creating Employee Setting firstName : Freddie Setting lastName : Mercury ..Creating Office ..Setting office description : Headquarters ....Creating Address ....Setting streetName : Wellington Avenue ....Setting streetNumber : 223 ..Setting office address : ....223 Wellington Avenue Adding Office to this employee ..Creating Office ..Setting office description : Client site ....Creating Address ....Setting streetName : Downing Street ....Setting streetNumber : 10 ..Setting office address : ....10 Downing Street Adding Office to this employee ------------------------------------------------- Headquarters Address : 223 Wellington Avenue -------------------------------- Client site Address : 10 Downing Street -------------------------------- Example 3 :
<employee firstName="Freddie" lastName="Mercury">
<office description="Headquarters">
<address streetName="Wellington Avenue" streetNumber="223"/>
</office>
<office description="Client site">
<address streetName="Downing Street" streetNumber="10"/>
</office>
</employee>
private Address address;
private String description;
public Office() {
System.out.println("..Creating Office");
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
System.out.println("..Setting office description : " + description);
this.description = description;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
System.out.println("..Setting office address : " + address);
this.address = address;
}
}
public class Address {
private String streetName;
private String streetNumber;
public Address() {
System.out.println("....Creating Address");
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
System.out.println("....Setting streetName : " + streetName);
this.streetName = streetName;
}
public String getStreetNumber() {
return streetNumber;
}
public void setStreetNumber(String streetNumber) {
System.out.println("....Setting streetNumber : " + streetNumber);
this.streetNumber = streetNumber;
}
public String toString() {
return "...." + streetNumber + " " + streetName;
}
}
String path = System.getProperty("user.dir") + File.separator + "etc";
File file = new File(path, "employee2.xml");
Digester digester = new Digester();
// add rules
digester.addObjectCreate("employee", "ex15.pyrmont.digestertest.Employee");
digester.addSetProperties("employee");
digester.addObjectCreate("employee/office", "ex15.pyrmont.digestertest.Office");
digester.addSetProperties("employee/office");
digester.addSetNext("employee/office", "addOffice");
digester.addObjectCreate("employee/office/address",
"ex15.pyrmont.digestertest.Address");
digester.addSetProperties("employee/office/address");
digester.addSetNext("employee/office/address", "setAddress");
try {
Employee employee = (Employee) digester.parse(file);
ArrayList offices = employee.getOffices();
Iterator iterator = offices.iterator();
System.out.println("-------------------------------------------------");
while (iterator.hasNext()) {
Office office = (Office) iterator.next();
Address address = office.getAddress();
System.out.println(office.getDescription());
System.out.println("Address : " +
address.getStreetNumber() + " " + address.getStreetName());
System.out.println("--------------------------------");
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
在给digester注入解析规则的时候,上面的两个例子是逐条注入的。还有一种方法是 集体注入。
实体对象沿用上例中的定义。
规则的集合定义在一个类中
EmployeeRoleSet.java
public class EmployeeRuleSet extends RuleSetBase {
public void addRuleInstances(Digester digester) {
// add rules
digester.addObjectCreate("employee", "ex15.pyrmont.digestertest.Employee");
digester.addSetProperties("employee");
digester.addObjectCreate("employee/office", "ex15.pyrmont.digestertest.Office");
digester.addSetProperties("employee/office");
digester.addSetNext("employee/office", "addOffice");
digester.addObjectCreate("employee/office/address",
"ex15.pyrmont.digestertest.Address");
digester.addSetProperties("employee/office/address");
digester.addSetNext("employee/office/address", "setAddress");
}
}
Test03.java
public class Test03 {
public static void main(String[] args) {
String path = System.getProperty("user.dir") + File.separator + "etc";
File file = new File(path, "employee2.xml");
Digester digester = new Digester();
digester.addRuleSet(new EmployeeRuleSet());//注入规则集合
try {
Employee employee = (Employee) digester.parse(file);
ArrayList offices = employee.getOffices();
Iterator iterator = offices.iterator();
System.out.println("-------------------------------------------------");
while (iterator.hasNext()) {
Office office = (Office) iterator.next();
Address address = office.getAddress();
System.out.println(office.getDescription());
System.out.println("Address : " +
address.getStreetNumber() + " " + address.getStreetName());
System.out.println("--------------------------------");
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
结果为:
Creating Employee Setting firstName : Freddie Setting lastName : Mercury ..Creating Office ..Setting office description : Headquarters ....Creating Address ....Setting streetName : Wellington Avenue ....Setting streetNumber : 223 ..Setting office address : ....223 Wellington Avenue Adding Office to this employee ..Creating Office ..Setting office description : Client site ....Creating Address ....Setting streetName : Downing Street ....Setting streetNumber : 10 ..Setting office address : ....10 Downing Street Adding Office to this employee ------------------------------------------------- Headquarters Address : 223 Wellington Avenue -------------------------------- Client site Address : 10 Downing Street -------------------------------- ContextConfig 下面 言归正传 , 通过讲解contextConfig,来看看tomcat的web.xml文件(两个,tomcat default一个,application一个)是怎么注入到context中的。 (待续...)
发表评论
-
Servlet Async
2015-05-15 02:29 465http://www.importnew.com/8864. ... -
EHcache
2014-09-06 23:32 495http://blog.csdn.net/liuzhenwe ... -
Maven构建并热部署一个简略的Web Project
2014-08-01 01:51 707zz from : http://www.myexcept ... -
Tomcat 权限配置,当使用Eclipse启动时都会被修改为默认
2014-07-31 07:57 514http://zhidao.baidu.com/link?u ... -
解决tomcat占用8080端口问题图文教程
2014-07-30 21:59 599zz from http://jingyan.baidu. ... -
HttpURLConnection用法详解
2013-09-04 02:08 644http://www.cnblogs.com/guodong ... -
使用HttpSessionListener接口监听Session 的创建和失效
2013-09-03 03:55 789http://uule.iteye.com/blog/824 ... -
web.xml 中的listener、 filter、servlet 加载顺序及其详解
2013-09-03 03:49 336http://www.cnblogs.com/JesseV/ ... -
servlet listener
2013-09-03 03:43 624http://sakyone.iteye.com/blog/ ... -
servlet filter
2013-09-03 03:42 678http://sakyone.iteye.com/blog/ ... -
Servlet中Request的getAttribute getParameter 区别
2013-08-29 12:31 1077http://blog.csdn.net/yangqillo ... -
用windows启动tomcat
2013-07-04 09:36 616services.msc -
Spring 加载 application.xml 出错FileNotFoundException
2013-06-23 21:26 919http://zhidao.baidu.com/questi ... -
<context-param>与<init-param>的区别与作用
2013-06-18 08:08 681http://www.cnblogs.com/hzj-/ar ... -
HttpWatch工具简介及使用技巧
2012-05-09 14:13 3882http://cfeng518.blog.163.com/bl ... -
Web Service (Axis2实现)
2012-04-19 11:13 1293转载from http://wangyaobeiji ... -
Tomcat4 源代码分析 (20) How Tomcat Works 第二十章
2009-07-27 17:24 875Chapter 20 : JMX-Based Manageme ... -
Tomcat4 源代码分析 (19) How Tomcat Works 第十九章
2009-07-27 17:19 753Chapter 19 : Manager Servlet ... -
Tomcat4 源代码分析 (18) How Tomcat Works 第十八章
2009-07-27 16:58 793Chapter 18 : Deployer T ... -
Tomcat4 源代码分析 (17) How Tomcat Works 第十七章
2009-07-24 17:57 1040Chapter 17 : Tomcat Startup ...
相关推荐
《How Tomcat Works》是一份深入探讨Apache Tomcat工作原理的重要资源,包含了英文PDF文档、中文HTML翻译以及源代码,旨在帮助读者理解Tomcat服务器的内部运作机制。这份资料是IT从业者,特别是Java Web开发者、系统...
在《How Tomcat Works》这本书中,作者详细剖析了Tomcat的内部工作机制,特别是对Tomcat的4.1.12和5.0.18两个版本的源代码进行了深入的分析。本书通过逐步揭示Tomcat的各个组件,帮助读者理解Tomcat的内部结构和运作...
《How Tomcat Works》这本书及其源代码,为开发者提供了深入理解Tomcat内部工作机制的宝贵资源。本文将基于提供的文件名"如何Tomcat工作",结合Tomcat的工作流程,深入探讨其核心组件和功能。 1. **Tomcat架构概述*...
通过阅读和理解Tomcat的源代码,可以深入了解其内部工作流程,从而提升自定义扩展和调试的能力。《HowTomcatWorksApps.zip》这个压缩文件可能包含了一些示例应用或代码片段,供读者实践和研究。 总的来说,《How ...
4. **JSP编译**:Tomcat在首次遇到JSP页面时,会将其转换为Servlet源代码并编译成.class文件。这个过程涉及JSP标签库、EL(Expression Language)和JSTL(JavaServer Pages Standard Tag Library)的解析。 5. **...
《How Tomcat Works》是一本深入解析Apache Tomcat服务器内部工作原理的重要参考资料,它提供了对Tomcat架构的全面理解,包括其设计、配置和优化。这本书的中文版和英文版都为读者提供了便利,无论你是母语为中文...
作为一本面向中高级开发者的书籍,《how tomcat works》不仅为读者提供了丰富的理论知识,还提供了许多实用的示例和源代码分析,帮助读者更好地理解概念,并应用到实践中去。例如,书中提到了如何为Tomcat实现自定义...
《HowTomcatWorks》是一本深入解析Apache Tomcat工作原理的书籍,中文版的发布使得更多的中国开发者能够理解和掌握这款广泛应用的开源Java Servlet容器的工作机制。Tomcat是Apache软件基金会Jakarta项目的一部分,它...
"how-tomcat-works-master_howtomcatworks_"是一个关于Tomcat工作原理的源码分析项目,经过整理后可以在IntelliJ IDEA中直接运行,为开发者提供了一手的实践平台。 首先,我们要了解Tomcat的核心组件。Tomcat主要由...
"HowTomcatWorks"项目,正如其名,旨在帮助开发者了解Tomcat的工作原理,通过源代码分享,使我们有机会深入探究这个强大的服务器内部机制。 1. **Tomcat架构概览** Tomcat的架构设计分为几个主要部分:Catalina...
《深入剖析Tomcat》这本书是Java开发者们了解和学习Tomcat服务器的重要参考资料,它详细解析了Tomcat的工作原理和内部机制。随书源码则为读者提供了实践和探索的平台,帮助读者更深入地理解书中的理论知识。下面将...
而"HowTomCatWorks"很可能是源码的目录或者解压后的源代码文件,读者可以直接查看和分析Tomcat的源代码,这对于深入学习和定制Tomcat非常有帮助。 总的来说,这份资料对于想要深入了解Tomcat、提升服务器性能的...
而"jakarta-tomcat-4.1.12-src.zip"和"jakarta-tomcat-5.0.18-src.zip"则包含了对应版本的源代码,这些源代码对于学习Tomcat的架构设计、模块划分以及代码实现非常关键。同时,源码包通常还包括构建脚本,读者可以...
【标题】"how tomcat works和jetty-src和tomcat7-src" 提及了三个关键概念:Tomcat的工作原理、Jetty源代码以及Tomcat 7的源代码。这些是Java Web服务器领域的核心元素,主要涉及到如何处理HTTP请求、部署Web应用...
how tomcat works 中文版 1-20章 现在全了 不光前四章 感谢 macula7 的空间 http://hi.baidu.com/macula7/blog/item/81fd14cbb7059f8dc81768d8.html 其翻译了 how tomcat works 还写了tomcat源代码学习
《How Tomcat Works》是一份详细阐述Tomcat工作原理的资源,包含英文版电子书和源代码。Tomcat是Apache软件基金会的项目,它是一个开源的Java Servlet容器,广泛用于部署Java Web应用程序。这份资料旨在帮助读者深入...
本篇文章将深入剖析"HowTomcatWorks",帮助读者全面理解Tomcat的内部工作机制。 1. **Tomcat架构概述** Tomcat由一系列组件构成,包括Catalina(核心Servlet容器)、Jasper(JSP引擎)、 Coyote(HTTP/HTTPS连接器...
通过阅读《HowTomcatWorks》这本书和分析相关的源代码,开发者可以更深入地理解Tomcat的工作原理,从而在实际开发中更有效地利用Tomcat,解决性能问题,提高系统的稳定性和可扩展性。对于希望成为高级Java Web开发...