思路:通過(guò)解析xml將各個(gè)類(lèi)存入容器內(nèi),然后通過(guò)map存放各個(gè)標(biāo)簽的內(nèi)容,通過(guò)
url-pattern的內(nèi)容找到servlet-name,通過(guò)servlet-name的內(nèi)容,找到對(duì)應(yīng)的類(lèi)servlet-class,然后通過(guò)servlet-class內(nèi)的內(nèi)容(包名.類(lèi)名)反射得到對(duì)應(yīng)的類(lèi),然后輸出
創(chuàng)新互聯(lián)提供網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì),品牌網(wǎng)站制作,廣告投放等致力于企業(yè)網(wǎng)站建設(shè)與公司網(wǎng)站制作,十載的網(wǎng)站開(kāi)發(fā)和建站經(jīng)驗(yàn),助力企業(yè)信息化建設(shè),成功案例突破成百上千,是您實(shí)現(xiàn)網(wǎng)站建設(shè)的好選擇.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>hello.RegisterServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>reg</servlet-name>
<servlet-class>hello.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
<url-pattern>/g</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>reg</servlet-name>
<url-pattern>/reg</url-pattern>
</servlet-mapping>
</web-app>
xml:
public class xmll {
public static void main(String[] args) throws Exception{
//SAX解析
//1、獲取解析工廠
SAXParserFactory factory=SAXParserFactory.newInstance();
//2、從解析工廠獲取解析器
SAXParser parse =factory.newSAXParser();
//3、編寫(xiě)處理器
//4、加載文檔 Document 注冊(cè)處理器
WebHandler handler=new WebHandler();
//5、解析
parse.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("hello\\web.xml")
,handler);
//獲取數(shù)據(jù)
WebContext context=new WebContext(handler.getEntitys(),handler.getMappings());
//假設(shè)你輸入了 /login
String className=context.getClz("/login");
//反射
Class clz=Class.forName(className); //className返回的是servlet-class的內(nèi)容,即包名.類(lèi)名
Servlet servlet=(Servlet)clz.getConstructor().newInstance();
servlet.service();
}
}
class WebHandler extends DefaultHandler{
private List<Entity> entitys = new ArrayList<Entity>();
private List<Mapping> mappings = new ArrayList<Mapping>();
private Entity entity ;
private Mapping mapping ;
private String tag; //存儲(chǔ)操作標(biāo)簽
private boolean isMapping = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(null!=qName) {
tag = qName; //存儲(chǔ)標(biāo)簽名
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對(duì)應(yīng)的內(nèi)容,不是標(biāo)簽,并且內(nèi)容不能相同
//因?yàn)橐粋€(gè)類(lèi)有多個(gè)url的內(nèi)容,所以加入集合,要不然要兩個(gè)變量來(lái)保存,且這兩個(gè)url都是對(duì)應(yīng)的一個(gè)name一個(gè)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<Entity> getEntitys() {
return entitys;
}
public List<Mapping> getMappings() {
return mappings;
}
}
webContext:
public class WebContext {
private List<Entity> entitys=null;
private List<Mapping> mappings=null;
//key:servlet-name value:servlet-class
private Map<String,String> entityMap=new HashMap<String,String>();
//key:url-pattern value :servlet-name
private Map<String,String> mappingMap=new HashMap<String,String>();
public WebContext(List<Entity> entitys,List<Mapping> 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<String> patterns ;
public Mapping() {
patterns = new HashSet<String>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<String> getPatterns() {
return patterns;
}
public void setPatterns(Set<String> patterns) {
this.patterns = patterns;
}
public void addPattern(String pattern) {
this.patterns.add(pattern);
}
}
新聞名稱(chēng):javawebserver-反射webxml
標(biāo)題來(lái)源:http://aaarwkj.com/article0/ipogoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、自適應(yīng)網(wǎng)站、企業(yè)網(wǎng)站制作、商城網(wǎng)站、標(biāo)簽優(yōu)化、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)