java的tomcat_JAVA程序获取Tomcat的运行状态
用浏览器来查看Tomcat的运行状态:
配置Tomcat的管理用户和权限。
打开%Tomcat_Home%/conf/目录下的tomcat-user.xml文件,配置以下内容:
此时可通过admin用户来访问tomcat的状态,在浏览器地址中输入:
http://localhost:8080/manager/status?XML=true
输入用户名:admin,密码:tomcat. 出现以下页面,即Tomcat的运行状态。
或者可以直接访问:
http://admin:tomcat@localhost :8080/manager/status?XML=true
Java程序获取Tomcat运行状态:
根据上面的思路,可以通过URL来获取Tomcat manager返回的信息,再解析这个信息就可以了,参考cacti的监控方式中,用perl编写的监控模板也是如此,如下:
my $host = shift;
my $username = shift;
my $password = shift;
my $connector = shift or &usage;
my $url = "http://$username:$password"."\@$host/manager/status?XML=true";
my $xml = `GET $url`;
my $status = XMLin($xml);
因此Java也可参考此方法:
@Test
public void test1() throws IOException {
URL url = null;
InputStream is = null;
StringBuffer resultBuffer = new StringBuffer();
BufferedReader breader = null;
try {
url = new URL(
"http://admin:tomcat@localhost:8080/manager/status?XML=true");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
is = conn.getInputStream();
breader = new BufferedReader(new InputStreamReader(is));
String line = "";
while ((line = breader.readLine()) != null) {
resultBuffer.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (breader != null)
breader.close();
if (is != null)
is.close();
}
System.out.println(resultBuffer.toString());
}
运行时出现如下错误:
java.io.IOException: Server returned HTTP response code: 401 for URL: http://admin:tomcat@localhost:8080/manager/status?XML=true
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
at com.merit.monitor.tomcat.TestTomcatStatus.test1(TestTomcatStatus.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
出错原因可以去stackoverview上查看,这里直接给出正确的解决方法:
1、编写获取Tomcat状态的字符串方法:
/**
* @Description: 获取指定URL的内容
* @Version1.0 2014-7-23 下午02:18:22 by xuqiang(xuqiang@merit.com)创建
* @param tempurl url地址
* @param username tomcat 管理用户名
* @param password tomcat 管理用户密码
* @return
* @throws IOException
*/
public static String getHtmlContext(String tempurl, String username, String password) throws IOException {
URL url = null;
BufferedReader breader = null;
InputStream is = null;
StringBuffer resultBuffer = new StringBuffer();
try {
url = new URL(tempurl);
String userPassword = username + ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());//在classpath中添加rt.jar包,在%java_home%/jre/lib/rt.jar
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Basic " + encoding);
is = conn.getInputStream();
breader = new BufferedReader(new InputStreamReader(is));
String line = "";
while ((line = breader.readLine()) != null) {
resultBuffer.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if(breader != null)
breader.close();
if(is != null)
is.close();
}
return resultBuffer.toString();
}
2、编写测试方法:
@Test
public void test() {
String result = "";
Document document = null;//引入org.dom4j包
try {
result = GetHtmlContext.getHtmlContext("http://localhost:8080/manager/status?XML=true", "admin", "tomcat");
document = DocumentHelper.parseText(result);//将字符串转化为XML的Document
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
System.out.println(document.asXML());
}
运行junit,正常打印监控状态的XML格式内容。这样就可以解析XML Dom的值获取Tomcat的运行状态。
java的tomcat_JAVA程序获取Tomcat的运行状态相关推荐
- JAVA程序获取Tomcat的运行状态
2019独角兽企业重金招聘Python工程师标准>>> 用浏览器来查看Tomcat的运行状态: 配置Tomcat的管理用户和权限. 打开%Tomcat_Home%/conf/目录下的 ...
- 一行Java代码竟能获取tomcat的绝对路径
场景 有时需要获取Tomcat服务器的绝对地址. 实现 System.getProperty("catalina.home"); 验证: 在后台action中添加 String p ...
- (详细版)java实现小程序获取微信登录,用户信息,手机号,头像
ps:springboot结合mybatisPlus.mysql实现,简单易懂,一件粘贴使用,详细往下看↓ 步骤: 1.注册微信开发平台账号,并创建小程序,获取小程序的AppID和AppSecret. ...
- Windows平台下tomcat+java的web程序持续占cpu问题调试
1.问题 Tomcat服务器跑了一段时间后,发现Tomcat进程占用的CPU资源在80%-100%间,加上其它的进程,整个服务器的CPU处理100%运行状态. 2.通过process explorer ...
- java osgi web开发_在Tomcat中使用Java Web应用程序的OSGi软件包
我试图从Java Web应用程序调用OSGi包的方法.两者都应该在Tomcat 7上运行. 我已经写了一个正常的Java应用程序,它调用OSGi软件包中的方法,如本站所述:http://drupal. ...
- java获取tomcat启动时间不对_部署在Tomcat 服务器中的web应用读取时间与系统时间不一致问题...
我在部署应用到Ubantu系统上的tomcat服务器中运行,发现操作系统的时间和tomcat中的应用程序获取的时间不一致,总是相差8个小时,但是查看当前操作系统的时区也是CST时区(中国标准时区). ...
- java中tomcat_Java中Tomcat各版本比较
Apache Tomcat 9.X Apache Tomcat 9.x的发展是当前的焦点.它建立在Tomcat 8.0.x.Tomcat 8.5 x和.x和实现Servlet 4.0.JSP 2.3. ...
- 在Tomcat中部署Java Web应用程序几种方式
在Tomcat中部署Java Web应用程序有两种方式:静态部署和动态部署.在下文中$CATALINA_HOME指的是Tomcat根目录. 一.静态部署 静态部署指的是我们在服务器启动之 ...
- 使用Maven,Jetty和Tomcat在嵌入式容器中运行Java Web应用程序
在开发Java Web应用程序时,从"真实"环境中获得快速反馈非常实用. 在本文中,我将探讨如何在嵌入式容器Jetty或Tomcat中运行带有Maven的Java Web应用程序. ...
最新文章
- CSDN 文章标题含非法字符
- iOS相关,过年回来电脑上的证书都失效了
- linux资源隔离是哪些,【转载】Linux cgroup资源隔离各个击破之
- Java远程连接阿里云MySQL_阿里云配置mysql远程连接
- pca 累积方差贡献率公式_PCA--主成分分析
- C# ToString格式字符串整理(Format)(数字、日期和枚举的标准格式设置说明符)
- linux HZ 值_Linux操作系统中进程的操作命令(ps,kill,keep)
- paip.提高效率---微信 手机app快速开发平台—微网络撬动大市场
- java 静态方法 非静态变量_深度分析:Java 静态方法/变量,非静态方法/变量的区别,今天一并帮你解决!...
- 无需代码,图文并茂详解EEGLAB中的ERPs数据分析方法
- java飞机大战boos代码_飞机大战 java 源代码
- 根据经纬度算距离 | SQL
- 创新抗生素获FDA优先审评资格 治疗社区获得性肺炎
- php get month,JavaScript从Date对象返回月份 (0 ~ 11)的方法getMonth()
- 关于消防设施的RFID资产管理,RFID消防设施资产管理-新导智能
- C++动态爱心 520 七夕情人节整活
- 微信开挂怎么防止封号_为什么我的微信老是被封解决办法 微信如何防止被封号详细介绍...
- 重启镜像DOCKER
- jQuery中index的用法
- 你不知道的东西----半月痕(中医诊断)