Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List deployed webapps in Apache Tomcat

Tags:

java

tomcat

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?

like image 763
galchen Avatar asked Oct 18 '11 10:10

galchen


People also ask

Where is the webapps directory Tomcat?

First, the packaged or exploded application is copied to the Tomcat "appBase", a directory which is configured on a per-Host basis. The default appBase location is "$CATALINA_BASE/webapps", or "$CATALINA_HOME/webapps", if no base directory has been defined.

How do I know if WAR is deployed in Tomcat?

The definitive way to determine if a war has finished deploying and the webapp has started is to look in the catalina. out log. A message will be logged. In operational terms, Tomcat will respond to a request made to that webapp in various ways depending on how far along it is.

What is Tomcat webapps?

The webapps directory is where deployed applications reside in Tomcat. The webapps directory is the default deployment location, but this can be configured with the appBase attribute on the <Host> element.

Where is WAR file deployed in Tomcat?

Go to your project Directory and inside Dist Folder you will get war file that you copy on your tomcat. webApp Folder.


2 Answers

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

like image 164
JRichardsz Avatar answered Sep 16 '22 13:09

JRichardsz


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. } 
like image 34
Stan Kurilin Avatar answered Sep 17 '22 13:09

Stan Kurilin