How do you introspect web.xml from a servlet?
-
16-09-2019 - |
Question
Is there a way for a servlet filter to get a list of all servlets and their mappings?
Solution
There is no standard API (anymore; and what was there was rather limited) - but it's an XML file with a standard schema. You can obtain it in your filter via:
filterConfig.getServletContext().getResource("/WEB-INF/web.xml");
and get what you want from it using SAX / DOM / XPath / what have you, e.g.
InputStream is = filterConfig.getServletContext()
.getResourceAsStream("/WEB-INF/web.xml");
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document document = builder.parse(is);
NodeList servlets = document.getElementsByTagName("servlet");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow