Apache Tomcat have different types of Users Role.
Role names in the web.xml file of the Manager web application. The available roles are:
manager-gui — Access to the HTML interface.
manager-status — Access to the “Server Status” page only.
manager-script — Access to the tools-friendly plain text interface .
manager-jmx — Access to JMX proxy interface and to the “Server Status” page.
To manage application like application reload , stop or start we need user with role manage-script.
Give role manage-script to user in tomcat-users.xml
e.g
<role rolename=”tomcat”/>
<role rolename=”admin-script”/>
<role rolename=”manager-gui”/>
<role rolename=”manager-script”/>
<user name=”admin” password=”admin” roles=”manager-gui,admin-script,tomcat,manager-script” />
Simple java code to get connection count of your application using java code.
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import sun.misc.BASE64Encoder;
public class RestartTomcat {
public static void main(final String[] args) {
try {
final URL url = new URL("http://localhost/manager/text/sessions?path=/manager");
final BASE64Encoder e = new BASE64Encoder();
final String encoding = e.encode("admin:admin".getBytes());
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + encoding);
final InputStream content = connection.getInputStream();
final BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
String lastLine = "";
System.out.println("Tomcat response: ");
while ((line = in .readLine()) != null) {
lastLine = line;
System.out.println(line);
}
final String[] parts = lastLine.split("minutes: ");
if (parts.length >= 2) {
System.out.println("\nSessions count is " + parts[1]);
} else {
System.out.println("zero session");
}
} catch (final Exception e) {
e.printStackTrace();
}
}
}
Output will be like this :
Tomcat response:
OK – Session information for application at context path /manager
Default maximum session inactive interval 30 minutes
1 – <2 minutes: 2 sessions
Sessions count is 2 sessions