在上一篇博客 XML解析webXML 的基础上,我们增加反射,通过url-pattern获取servlet-name然后通过servlet-name获取servlet-class,servlet-class就是类的路径,就可以反射创建对象实例
测试使用的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.server.basic.servlet.LoginSerlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
<url-pattern>/g</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>reg</servlet-name>
<servlet-class>com.server.basic.servlet.RegisterSerxlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>reg</servlet-name>
<url-pattern>/reg</url-pattern>
</servlet-mapping>
</web-app>
定义servlet接口继承之并创建LoginSerlet和RegisterSerxlet类
package com
.server
.basic
.servlet
;
public interface servlet {
public void service();
}
package com
.server
.basic
.servlet
;
public class LoginSerlet implements servlet{
public void service() {
System
.out
.println("LoginSerlet");
}
}
package com
.server
.basic
.servlet
;
public class RegisterSerxlet implements servlet{
public void service() {
System
.out
.println("RegisterSerxlet");
}
}
定义webContent类用于获取servlet-class,增加数据成员entitys和mappings将list转为map便于查找
import java
.util
.HashMap
;
import java
.util
.List
;
import java
.util
.Map
;
import java
.util
.Set
;
public class webContent {
private List
<Entity> entitys
;
private List
<Mapping> mappings
;
private Map
<String, String> entitysMap
= new HashMap<String, String>();
private Map
<String, String> mappingsMap
= new HashMap<String, String>();
public webContent(List
<Entity> entitys
, List
<Mapping> mappings
) {
super();
this.entitys
= entitys
;
this.mappings
= mappings
;
for(Entity e
:entitys
)
entitysMap
.put(e
.getNameString(), e
.getClas());
for(Mapping m
:mappings
) {
Set
<String> set
= m
.getPatterns();
for(String patt
:set
)
mappingsMap
.put(patt
, m
.getName());
}
}
public String
GetClas(String pattern
) {
return entitysMap
.get(mappingsMap
.get(pattern
));
}
}
Mapping类
import java
.util
.Set
;
import java
.util
.HashSet
;
public class Mapping {
private String name
;
private Set
<String> patterns
;
public Mapping() {
patterns
= new HashSet<String>();
}
@Override
public String
toString() {
String tmpString
= "\n";
for(String s
:patterns
)
tmpString
+= s
+ "\n";
return "Mapping [name=" + name
+ ", patterns=" + tmpString
+ "]";
}
public String
getName() {
return name
;
}
public Set
<String> getPatterns() {
return patterns
;
}
public void setName(String name
) {
this.name
= name
;
}
public void addPatterns(String pattern
) {
this.patterns
.add(pattern
);
}
}
Entity类
*
* Entity类用于存储以下xml解析的数据:
* <servlet>
* <servlet
-name
>login
</servlet
-name
>
* <servlet
-class>com
.shsxt
.LoginSerlet
</servlet
-class>
* </servlet
>
* ...
*/
public class Entity {
private String nameString
;
private String clas
;
public Entity() {
}
@Override
public String
toString() {
return "Entity [nameString=" + nameString
+ ", clas=" + clas
+ "]";
}
public String
getNameString() {
return nameString
;
}
public String
getClas() {
return clas
;
}
public void setNameString(String nameString
) {
this.nameString
= nameString
;
}
public void setClas(String clas
) {
this.clas
= clas
;
}
}
XMLWeb
package com
.server
.basic
.servlet
;
import java
.io
.IOException
;
import java
.lang
.reflect
.InvocationTargetException
;
import java
.util
.ArrayList
;
import java
.util
.List
;
import javax
.xml
.parsers
.ParserConfigurationException
;
import javax
.xml
.parsers
.SAXParser
;
import javax
.xml
.parsers
.SAXParserFactory
;
import org
.xml
.sax
.Attributes
;
import org
.xml
.sax
.SAXException
;
import org
.xml
.sax
.helpers
.DefaultHandler
;
public class XMLWeb {
public static void main(String
[] args
) throws ParserConfigurationException
, SAXException
, IOException
, ClassNotFoundException
, InstantiationException
, IllegalAccessException
, IllegalArgumentException
, InvocationTargetException
, NoSuchMethodException
, SecurityException
{
SAXParserFactory factory
= SAXParserFactory
.newInstance();
SAXParser parse
= factory
.newSAXParser();
WebHandler handler
= new WebHandler();
parse
.parse(Thread
.currentThread().getContextClassLoader().getResourceAsStream("com/server/basic/servlet/web.xml"), handler
);
webContent wc
= new webContent(handler
.getEntitys(), handler
.getMappings());
String classRoad
= wc
.GetClas("/login");
Class
c = Class
.forName(classRoad
);
servlet serv
= (servlet
)c
.getConstructor().newInstance();
serv
.service();
classRoad
= wc
.GetClas("/g");
c
= Class
.forName(classRoad
);
serv
= (servlet
)c
.getConstructor().newInstance();
serv
.service();
classRoad
= wc
.GetClas("/reg");
c
= Class
.forName(classRoad
);
serv
= (servlet
)c
.getConstructor().newInstance();
serv
.service();
}
}
class WebHandler extends DefaultHandler{
private List
<Entity> entitys
= new ArrayList<>();
private List
<Mapping> mappings
= new ArrayList<>();
private Entity entity
;
private Mapping mapping
;
private String tag
;
private boolean isMapping
= false;
public List
<Entity> getEntitys() {
return entitys
;
}
public List
<Mapping> getMappings() {
return mappings
;
}
@Override
public void characters(char[] ch
, int start
, int length
) throws SAXException
{
String contents
= new String(ch
, start
, length
).trim();
if(null
!= tag
) {
if(isMapping
) {
if(tag
.equals("servlet-name"))
mapping
.setName(contents
);
else if(tag
.equals("url-pattern"))
mapping
.addPatterns(contents
);
}else {
if(tag
.equals("servlet-name"))
entity
.setNameString(contents
);
else if(tag
.equals("servlet-class"))
entity
.setClas(contents
);
}
}
}
@Override
public void startElement(String uri
, String localName
, String qName
, Attributes attributes
) throws SAXException
{
if(null
!= qName
) {
tag
= qName
;
if(qName
.equals("servlet")) {
entity
= new Entity();
isMapping
= false;
}else if(qName
.equals("servlet-mapping")) {
mapping
= new Mapping();
isMapping
= true;
}
}
}
@Override
public void endElement(String uri
, String localName
, String qName
) throws SAXException
{
if(null
!= qName
) {
if(qName
.equals("servlet")) {
entitys
.add(entity
);
}else if(qName
.equals("servlet-mapping")) {
mappings
.add(mapping
);
}
}
tag
= null
;
}
}
通过传入不同的字符串(url-pattern)就可以通过反射创建不同的对象