Come si fa introspezione web.xml da un servlet?
-
16-09-2019 - |
Domanda
C'è un modo per un filtro servlet per ottenere un elenco di tutti i servlet e le loro mappature?
Soluzione
Non v'è alcuna API standard (più; e quello che c'era era piuttosto limitata) - ma si tratta di un file XML con uno schema standard. È possibile ottenere nel vostro filtro tramite:
filterConfig.getServletContext().getResource("/WEB-INF/web.xml");
e ottenere ciò che si vuole da utilizzando SAX / DOM / XPath / quello che hai, per es.
InputStream is = filterConfig.getServletContext()
.getResourceAsStream("/WEB-INF/web.xml");
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document document = builder.parse(is);
NodeList servlets = document.getElementsByTagName("servlet");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow