请求响应式编程 涉及:
OOP容器I/O多线程网络编程XML解析反射HTMLHTTP理解: 1、原来new对象需要JVM在类加载器中加载类模子,现在由使用者准备该模子 2、java类中的各种结构(方法、属性、构造器、类名)映射成一个个的java对象。利用反射可以对一个类进行解剖,反射是框架设计的灵魂。
/** * 反射:java类中的各种结构(方法、属性、构造器、类名)映射成一个个的java对象。 * 利用反射可以对一个类进行解剖,反射是框架设计的灵魂。 * 1、获取Class对象 * 三种方式:推荐Class.forName("包名.类名") * 2、可以动态创建 * clz.getConstructor().newInstance(); * */ public class ReflectTest { public static void main(String[] args) throws Exception { //三种方式 //1、对象.getClass() Class clz=new Iphone().getClass(); //2、类.class() clz=Iphone.class; //3、Class.forName("包名.类名") clz=Class.forName("com.sxt.server.basic.Iphone");//灵活性提高,编译的时候该类不需要存在,可以动态的传递。后期一版传子类 //创建对象 /*Iphone iphone2=(Iphone) clz.newInstance();//jdk9不推荐 System.out.println(iphone2);*/ //jdk9推荐 Iphone iphone2=(Iphone) clz.getConstructor().newInstance(); System.out.println(iphone2); } } class Iphone{ public Iphone() { } }解析:
<?xml version="1.0" encoding="UTF-8" ?> <persons> <person> <name>至尊宝</name> <age>9000</age> </person> <person> <name>白晶晶</name> <age>7000</age> </person> </persons>代码为:
public class XmlTest02 { public static void main(String[] args) throws Exception { //SAX解析 //1、获取解析工厂 SAXParserFactory factory=SAXParserFactory.newInstance(); //2、从解析工厂获取解析器 SAXParser parse =factory.newSAXParser(); //3、编写处理器 //4、加载文档Document注册处理器 PersonHandler handler=new PersonHandler(); //5、解析 parse.parse(Thread.currentThread().getContextClassLoader() .getResourceAsStream("com/sxt/server/basic/p.xml") ,handler); handler.printPersons(); } } class PersonHandler extends DefaultHandler { private List<Person> persons; private Person person; private String tag;//存储操作的标签 @Override public void startDocument() throws SAXException { persons=new ArrayList<Person>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println(qName+"---->解析开始"); if(null!=qName) { tag=qName; if(qName.equals("person")) { person=new Person(); } } } @Override public void characters(char[] ch, int start, int length) throws SAXException { String contents=new String(ch,start,length).trim(); if(null!=tag) { if(tag.equals("name")) { person.setName(contents); }else if(tag.equals("age")) { person.setAge(Integer.valueOf(contents)); } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println(qName+"---->解析结束"); if(null!=qName) { if(qName.equals("person")) { persons.add(person); } } tag=null; } @Override public void endDocument() throws SAXException { } public void printPersons() { for(Person p:persons) { System.out.println(p); } } }web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>login</servlet-name> <servlet-class>com.server.basic.servlet.LoginServlet</servlet-class> </servlet> <servlet> <servlet-name>reg</servlet-name> <servlet-class>com.server.basic.servlet.RegisterServlet</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元素的Entity类:
/** *<servlet> * <servlet-name>login</servlet-name> * <servlet-class>com.sxt.server.basic.servlet.LoginServlet</servlet-class> *</servlet> * */ public class Entity { private String name; private String clz; public Entity() { super(); } 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; } }存储xml元素的mapping类:
/** *<servlet-mapping> * <servlet-name>login</servlet-name> * <url-pattern>/login</url-pattern> * <url-pattern>/g</url-pattern> *</servlet-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); } }服务接口:
public interface Servlet { void service(); }实现类login:
public class LoginServlet implements Servlet { @Override public void service() { System.out.println("LoginServlet"); } }实现类Register
public class RegisterServlet implements Servlet { @Override public void service() { System.out.println("RegisterServlet"); } }数据整合上下文:
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; // 将entitys转成了对应的map for (Entity entity : entitys) { entityMap.put(entity.getName(), entity.getClz()); } // 将mappings转成了对应的map for (Mapping mapping : mappings) { for (String pattern : mapping.getPatterns()) { mappingMap.put(pattern, mapping.getName()); } } } /** * 通过URL的路径,找到对应的class * @param pattern * @return */ public String getClz(String pattern) { String name = mappingMap.get(pattern); if (null != name) { return entityMap.get(name); } return null; } }运行
public class XmlTest02 { 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("com/server/basic/servlet/web.xml"), handler); //获取数据 WebContext context=new WebContext(handler.getEntitys(),handler.getMappings()); //假设输入了/login String className=context.getClz("/g"); Class clz=Class.forName(className); Servlet servlet=(Servlet) clz.getConstructor().newInstance();//接口 System.out.println(servlet); servlet.service(); } } class WebHandler extends DefaultHandler { private List<Entity> entitys; private List<Mapping> mappings; private Entity entity; private Mapping mapping; private String tag;// 存储操作的标签 private boolean isMapping = false; @Override public void startDocument() throws SAXException { entitys = new ArrayList<Entity>(); mappings = new ArrayList<Mapping>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println(qName + "---->解析开始"); 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 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.addPattern(contents); } } else { 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 { System.out.println(qName + "---->解析结束"); if (null != qName) { if (qName.equals("servlet")) { entitys.add(entity); }else if(qName.equals("servlet-mapping")) { mappings.add(mapping); } } tag = null; } @Override public void endDocument() throws SAXException { } public List<Entity> getEntitys() { return entitys; } public List<Mapping> getMappings() { return mappings; } }