package com.hy.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; @WebServlet("*.do")
public class DispatcherServlet extends HttpServlet {
private Map<String, Object> map = new ConcurrentHashMap<>(); public DispatcherServlet() {
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("applicationContext.xml");
// 1,通过工厂模式,创建documentBuilderFactory工厂对象
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); // 2,创建DocumentBuilder对象
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); // 3,得到Document对象( 注意导入org.w3c.dom包中的)
Document document = documentBuilder.parse(inputStream); // 4,获得所有的bean标签
NodeList nodeList = document.getElementsByTagName("bean");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) {
//(注意导入org.w3c.dom包中的)
//强转成Element类的对象,里面有比Node类更方便的方法
Element element = (Element) node; String id = element.getAttribute("id");
String className = element.getAttribute("class");
boolean flag = map.containsKey(id);
if (flag == true)
return;
Object xxxController = Class.forName(className).newInstance();
map.put(id, xxxController);
}
}
} catch (ParserConfigurationException | SAXException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
} @Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); // 假设url是: http://localhost:8080/mymvc2/hello.do
// ServletPath是Servlet的访问路径: /hello.do
// 思路是:
// 第1步: /hello.do -> hello 或者 /book.do -> book
// 第2步: hello -> HelloController 或者 book -> BookController
String servletPath = request.getServletPath(); // /hello.do
int lastDotIndex = servletPath.lastIndexOf(".do");
servletPath = servletPath.substring(1, lastDotIndex); // hello
}
}

最新文章

  1. S2---深入.NET平台和C#编程的完美总结
  2. CRC 冗余校验计算
  3. OAuth的机制原理讲解及开发流程
  4. ubuntu安装StarDict星际译王
  5. 创建git标签【转】
  6. PCB阻抗调节
  7. php substr,iconv_substr,mb_substr
  8. Dede文章列表
  9. JVM JMM
  10. Android Java混淆(ProGuard)
  11. xhprof failed to execute cmd: &quot; dot -Tpng&quot;. stderr: `sh: dot: command not found &#39;
  12. dbf导入sqlserver的方法
  13. 32.APP后端处理表情的一些技巧
  14. 学习Python第七天
  15. [转]认识JWT
  16. 移动端滑屏全应用【四】移动端动画贞动画函数mTween封装
  17. openstack之~glance安装部署
  18. ECS API
  19. mysql 5.7.12 新增 X plugin x 协议 详解
  20. BeanUtils出现Java.lang.NoClassDefFoundError解决

热门文章

  1. 2022年7月15日,第四组,周鹏,JAVA认识的第三天,算法的第一天(╥╯^╰╥)(╥╯^╰╥)
  2. [R语言] 基于R语言实现环状条形图的绘制
  3. 使用 GPG 签名提交
  4. Python Excel 追加数据
  5. Unix 系统数据文件那些事儿
  6. [Leetcode]寻找峰值
  7. 网络安全之frp内网穿透
  8. Coolify系列02-从0到1超详细手把手教你上手Coolify
  9. webSocket前端+nodejs后端简单案例多人在线聊天
  10. 如何解决github下载很慢的问题?(已经解决)