views:

110

answers:

1

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

+4  A: 

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");
ChssPly76