package 手写服务器httpserver_206_XML配置文件_sax解析应用_练习;
/**
- 实体类1 保存
- login 手写服务器httpserver_206_XML配置文件_sax解析应用_练习.LoginServlet
*/
public class Entity {
private String name;
private String clz;//clz指令
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;
}
}
//2.------------实体类2--------------------------------
package 手写服务器httpserver_206_XML配置文件_sax解析应用_练习;
import java.util.ArrayList;
import java.util.List;
/**
- 实体类2 保存
- login /login login /log
*/
public class Mapping {
private String name;
private List urlPattern;//urlpattern 路径,映射路径
//空参
public Mapping() {//初始化
urlPattern = new ArrayList();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getUrlPattern() {
return urlPattern;
}
public void setUrlPattern(List urlPattern) {
this.urlPattern = urlPattern;
}
}
//-----------3--------------------------
<servlet>
<servlet-name>reg</servlet-name>
<servlet-class>手写服务器httpserver_206_XML配置文件_sax解析应用_练习.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>reg</servlet-name>
<url-pattern>/reg</url-pattern>
</servlet-mapping>
//--------------4-------------------------------
package 手写服务器httpserver_206_XML配置文件_sax解析应用_练习;
import java.util.Map;
import java.util.List;
/**
- 网页应用
*/
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class WebApp {
private static ServletContext contxt;
static {
try {//206课
//1.获取解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
//2.获取解析器
SAXParser sax = factory.newSAXParser();
//3.指定xml+处理器
WebHandier web = new WebHandier();
sax.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("手写服务器httpserver_206_XML配置文件_sax解析应用_练习/web.xml")
,web);
//将List转成Map
contxt = new ServletContext(); //模拟
Map<String, String> servlet = contxt.getServlet(); //204这里存的是servlet对象;改成存String字符串\再去ServletContext改
//存的是servlet-name与servlet-class
for(Entity entity:web.getEntityList()){
servlet.put(entity.getName(),entity.getClz());//依次存进
}
// 存url-pattern 与servlet-name(变成值)
Map<String, String> mapping = contxt.getMapping();
for(Mapping mapp:web.getMappingList()){
List<String> urls = mapp.getUrlPattern();
for(String url:urls){
mapping.put(url, mapp.getName());
}
}
} catch (Exception e) {
//e.printStackTrace();
}
}
public static Servlet getServlet(String url) throws InstantiationException, IllegalAccessException, ClassNotFoundException{//通过url获取不同的数据
if((null==url)||(url.trim()).equals("")){//=空就不用处理
}
//204课;获取 根据字符串(完整路径)创建对象
//return contxt.getServlet().get(contxt.getMapping().get(url));//否则就从contxt里面获取url
//return null;
//204课;先获取完整路径(字符串)并匹配好(LoginServlet)
String name = contxt.getServlet().get(contxt.getMapping().get(url));
//根据完整路径;动态获取创建对象
return (Servlet)Class.forName(name).newInstance();//在动态使用时获取对象;强制转换;处理异常;//一定确保空构造存在
}
}
//----------------5--------------------
package 手写服务器httpserver_206_XML配置文件_sax解析应用_练习;
import java.io.IOException;
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;
/**
- WebHandier网络处理程序
*/
public class WebHandier extends DefaultHandler{//DefaultHandler默认处理
//将Entity与Mapping保存进来
private List entityList;
private List mappingList;
private String beginTag;//beginTag开始标签
private boolean isMap;//为了处理方便使用
private Entity entity;
private Mapping mapping;
//重写
@Override
public void startDocument() throws SAXException {//StartDocument文档的开始
//文档解析开始 两个容器
entityList = new ArrayList();
mappingList = new ArrayList();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {//StartElement 元素的开始
//开始元素
if(null !=qName){
beginTag = qName;
if(qName.equals(“servlet”)){
isMap = false;
entity = new Entity();
}else if(qName.equals(“servlet-mapping”)){
isMap = true;
mapping = new Mapping();
}
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {//正则表达式字符(characters);处理内容
//处理内容
if(null !=beginTag ){
String str = new String( ch,start,length);
if(isMap){
if(beginTag.equals(“servlet-name”)){
mapping.setName(str);
}else if(beginTag.equals(“url-pattern”)){
//如果多个
mapping.getUrlPattern().add(str);
}
}else {
if(beginTag.equals(“servlet-name”)){
entity.setName(str);
}else if(beginTag.equals(“servlet-class”)){
entity.setClz(str);
}
}
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {//EndElement 元素的结束
//结束元素
if(null !=qName){
if(qName.equals(“servlet”)){
//加到容器里面
entityList.add(entity);
}else if(qName.equals(“servlet-mapping”)){
mappingList.add(mapping);
}
}
beginTag = null;
}
@Override
public void endDocument() throws SAXException {//EndDocument 文档的结束
//文档结束
}
//测试是否正确
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
}
public List<Entity> getEntityList() {
return entityList;
}
public void setEntityList(List<Entity> entityList) {
this.entityList = entityList;
}
public List<Mapping> getMappingList() {
return mappingList;
}
public void setMappingList(List<Mapping> mappingList) {
this.mappingList = mappingList;
}
}
//---------------------------------------------
//
//测试结果