35

I need to get a list of deployed webapps in Apache Tomcat. Also, for each webapp I need to get a list of initialized servlets and JSPs. Any ideas how this can be done?

I've found that the directory \tomcat\work\Catalina\localhost\ contains a child directory for each webapp. Could there be any other deployed webapps that aren't present there?

hopper
  • 13,060
  • 7
  • 49
  • 53
galchen
  • 5,252
  • 3
  • 29
  • 43

7 Answers7

25

In order to get a list of deployed apps of your tomcat you just need configure user/roles and use /manager/text/list tomcat endpoint

Configure user and roles in Tomcat

Add this in your /.../.../TOMCAT_HOME/conf/tomcat-users.xml

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>

<user username="my_user" password="my_pass" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>

You could skip "admin-gui" & "admin-script" roles if you will not perform admin operations.

After that, restart tomcat

List apps using browser

Go to your favorite browser and enter this url:

http://some_ip:some_port/manager/text/list

A login will appear. Enter the user/password configured in your TOMCAT_HOME/conf/tomcat-users.xml

Using command line

Just execute this:

curl -v -u my_user:my_pass http://127.0.0.1:some_port/manager/text/list

The result should be:

OK - Listed applications for virtual host localhost
/manager:running:0:manager
/:running:0:ROOT
/docs:running:0:docs
/examples:running:0:examples
/host-manager:running:0:host-manager
/my_app:running:0:my_app
/my_other_app:running:0:my_other_app
....
* Connection #0 to host 127.0.0.1 left intact

List apps with curl is used by a plugins related to automated tomcat deploys (Devops)

HTH

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
11

You can do it by using javax.management. It will look like

private Iterable<String> collectAllDeployedApps() {
    try {
        final Set<String> result = new HashSet<>();
        final Set<ObjectName> instances = findServer()
                .queryNames(new ObjectName("Tomcat:j2eeType=WebModule,*"), null);
        for (ObjectName each : instances) {
            result.add(substringAfterLast(each.getKeyProperty("name"), "/")); //it will be in format like //localhost/appname 
        }
        return result;
    } catch (MalformedObjectNameException e) {
         //handle
    }
}

private MBeanServer findServer() {
    ArrayList<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
    for (MBeanServer eachServer : servers) {
        for (String domain : eachServer.getDomains()) {
            if (domain.equals("Tomcat")) {
                return eachServer;
            }
        }
    }
    //handle. We are not in Tomcat.
}
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
  • 6
    In Tomcat 7+, you might need to change "Tomcat" to "Catalina" in order for the sample code to work. Once making that change, the sample code worked perfectly. – William Brendel Aug 18 '14 at 01:10
  • @WilliamBrendel - I have a similar issue, but in my case I cannot login (I'm instrumenting the application) - and therefore the list of apps is always empty. Any idea? My question in detail can be found here - http://stackoverflow.com/questions/39488852/tomcat-7-get-the-application-name-during-runtime-without-login-via-java-agent – Lin Sep 15 '16 at 07:47
5

Manual quote: List_Currently_Deployed_Applications

http://localhost:8080/manager/text/list
tsohr
  • 865
  • 2
  • 15
  • 25
3

I don't know how to do it. But there is a manager application in the standard distribution of tomcat, which lists the deployed apps. Sourcecode is provided, so you could have a look there.

Regarding your question about other webapps: Yes, there could be other webapps, which resides not in the webapp directory. They could be specified in the server.xml.

Andreas
  • 1,183
  • 1
  • 11
  • 24
2

The best way I found for information about the deployed applications and their content (including servlets, files, connections and such) is to install Lambda Probe in addition to whatever the tomcat instance is serving.

0xCAFEBABE
  • 5,576
  • 5
  • 34
  • 59
-1

Check out the Apache Tomcat manual. Section 5 is about the Manager application in Tomcat, which does just that.

SPee
  • 656
  • 4
  • 5
  • i did have a look there, but /manager/list probably belongs to previous version. anyways, i need to get the list form inside a filter – galchen Oct 18 '11 at 13:40
-2

http://code.google.com/p/psi-probe/wiki/Features seems like a good place to look.

Ray Hulha
  • 10,701
  • 5
  • 53
  • 53