javawebserver-反射webxml-创新互联
思路:通过解析xml将各个类存入容器内,然后通过map存放各个标签的内容,通过
url-pattern的内容找到servlet-name,通过servlet-name的内容,找到对应的类servlet-class,然后通过servlet-class内的内容(包名.类名)反射得到对应的类,然后输出
web.xml
login
hello.RegisterServlet
reg
hello.LoginServlet
login
/login
/g
reg
/reg
xml:
public class xmll {
public static void main(String[] args) throws Exception{
//SAX解析
//1、获取解析工厂
SAXParserFactory factory=SAXParserFactory.newInstance();
//2、从解析工厂获取解析器
SAXParser parse =factory.newSAXParser();
//3、编写处理器
//4、加载文档 Document 注册处理器
webHandler handler=new WebHandler();
//5、解析
parse.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("hello\\web.xml")
,handler);
//获取数据
webContext context=new WebContext(handler.getEntitys(),handler.getMappings());
//假设你输入了 /login
String className=context.getClz("/login");
//反射
Class clz=Class.forName(className); //className返回的是servlet-class的内容,即包名.类名
Servlet servlet=(Servlet)clz.getConstructor().newInstance();
servlet.service();
}
}
class WebHandler extends DefaultHandler{
private List entitys = new ArrayList();
private List mappings = new ArrayList();
private Entity entity ;
private Mapping mapping ;
private String tag; //存储操作标签
private boolean isMapping = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(null!=qName) {
tag = qName; //存储标签名
if(tag.equals("servlet")) {
entity = new Entity();
isMapping = false;
}else if(tag.equals("servlet-mapping")) {
mapping = new Mapping();
isMapping = true;
}
}
}
@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) { //操作servlet-mapping
if(tag.equals("servlet-name")) {
mapping.setName(contents);
}else if(tag.equals("url-pattern")) {
mapping.addPattern(contents); //添加的是url对应的内容,不是标签,并且内容不能相同
//因为一个类有多个url的内容,所以加入集合,要不然要两个变量来保存,且这两个url都是对应的一个name一个class
}
}else { //操作servlet
if(tag.equals("servlet-name")) {
entity.setName(contents);
}else if(tag.equals("servlet-class")) {
entity.setClz(contents);
}
}
}
}
@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; //tag丢弃了
}
public List getEntitys() {
return entitys;
}
public List getMappings() {
return mappings;
}
}
webContext:
public class WebContext {
private List entitys=null;
private List mappings=null;
//key:servlet-name value:servlet-class
private Map entityMap=new HashMap();
//key:url-pattern value :servlet-name
private Map mappingMap=new HashMap();
public WebContext(List entitys,List mappings)
{
this.entitys=entitys;
this.mappings=mappings;
for(Entity entity:entitys)
{
entityMap.put(entity.getName(), entity.getClz());
}
for(Mapping mapping:mappings)
{
for(String pattern:mapping.getPatterns())
{
mappingMap.put(pattern, mapping.getName());
}
}
}
public String getClz(String pattern)
{
String name=mappingMap.get(pattern);
return entityMap.get(name);
}
}
Servlet:
public interface Servlet {
void service();
}
LoginServlet:
public class LoginServlet implements Servlet {
public void service()
{
System.out.println("LoginServlet");
}
}
RegisterServlet:
public class RegisterServlet implements Servlet {
public void service()
{
System.out.println("RegisterServlet");
}
}
Entity
public class Entity {
private String name;
private String clz;
public Entity() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClz() {
return clz;
}
public void setClz(String clz) {
this.clz = clz;
}
}
Mapping
public class Mapping {
private String name;
private Set patterns ;
public Mapping() {
patterns = new HashSet();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getPatterns() {
return patterns;
}
public void setPatterns(Set patterns) {
this.patterns = patterns;
}
public void addPattern(String pattern) {
this.patterns.add(pattern);
}
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
标题名称:javawebserver-反射webxml-创新互联
链接分享:http://ybzwz.com/article/dejgpe.html