tags:

views:

44

answers:

3

Is it possible to get a list of all available servlets in an app?

I'm writing a web application that will expose a lot of servlets and I want to use HttpUnit to test each one to see if it is returning (or not!).

As lot of it will return a simple XML response, it would be really helpful not to write a test case for each one, only verify that it is working (a database change has not stopped this servlet to work, for example).

A: 

Using reflection you can look for all classes that are a HTTPServlet.

Daniel Moura
I don't think you can use reflection to see implementations of HTTPServlet. I know that it's not possible to get instances of a class/interface as the class loaded doesn't know about them until they're loaded.
Steve Kuo
+1  A: 

Don't think there is something out of the box. However you could write a small programm which parses your web.xml to gain the needed information.

kukudas
A: 

If you use tomcat, you can include the server jars in your classpath, and then cast the ServletContext to the Tomcat implementation. It contains everything you need.

Daniel
As of tomcat 5.5, it is not available. Both getServletNames() and getServlets() are deprecated, with no replacement, and always return an empty enumeration. See http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletContext.html#getServlets()
Fernando Pinheiro
You cannot use the public APIs, just the internal APIs. This is why I saif "include the server jars", that jars in $TOMCAT_HOME/server/lib
Daniel