How do you introspect web.xml from a servlet?

Is there a way for a servlet filter to get a list of all servlets and their mappings?

+5
source share
1 answer

There is no standard API (more, and that was pretty limited there), but it is an XML file with a standard schema. You can get it in your filter through:

filterConfig.getServletContext().getResource("/WEB-INF/web.xml");

and get from it what you want using SAX / DOM / XPath / what you have, for example

 InputStream is = filterConfig.getServletContext()
   .getResourceAsStream("/WEB-INF/web.xml");
 DocumentBuilder builder = DocumentBuilderFactory.newInstance()
   .newDocumentBuilder();
 Document document = builder.parse(is);
 NodeList servlets = document.getElementsByTagName("servlet");
+4
source

All Articles