web.xml
<?xml version="1.0"?> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>application</param-name> <param-value> org.apache.struts.example.ApplicationResources </param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> </servlet>
ServletBean.java
package digesterStruts; import java.util.HashMap; import java.util.Map; public class ServletBean { private String servletName; private String servletClass; private Map<String, String> initParamMap = new HashMap<String, String>(); public String getServletName() { return servletName; } public void setServletName(String servletName) { this.servletName = servletName; } public String getServletClass() { return servletClass; } public void setServletClass(String servletClass) { this.servletClass = servletClass; } public Map<String, String> getInitParamMap() { return initParamMap; } public void setInitParamMap(Map<String, String> initParamMap) { this.initParamMap = initParamMap; } public void addInitParamMap(String key, String value) { this.initParamMap.put(key, value); } }
Client.java
package digesterStruts; import java.io.IOException; import org.apache.commons.digester.Digester; import org.xml.sax.SAXException; public class Client { /** * @param args * @throws SAXException * @throws IOException */ public static void main(String[] args) throws IOException, SAXException { Digester digester = new Digester(); digester.setValidating(false); digester.addObjectCreate("servlet", ServletBean.class); digester.addSetProperties("servlet"); digester.addCallMethod("servlet/servlet-name", "setServletName", 0); digester.addCallMethod("servlet/servlet-class", "setServletClass" , 0); digester.addCallMethod("servlet/init-param", "addInitParamMap" , 2); digester.addCallParam("servlet/init-param/param-name", 0); digester.addCallParam("servlet/init-param/param-value", 1); ServletBean servletBean = (ServletBean) digester.parse(Client.class.getResourceAsStream("web.xml")); System.out.println(servletBean.getServletName()); System.out.println(servletBean.getServletClass()); System.out.println(servletBean.getInitParamMap()); } }