Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 864
jsight Avatar asked Oct 14 '22 14:10

jsight


1 Answers

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");
like image 59
ChssPly76 Avatar answered Oct 19 '22 18:10

ChssPly76