2.3 在DispatcherServlet的构造方法中解析applicationContext.xml配置文件
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 } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南