<%@ page language="java" import="java.io.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<title>shell</title>
</head>
<body>
<%
String cmds = "monitor.sh";
try {
Process process;
process = Runtime.getRuntime().exec("sh /opt/wzx/monitor.sh");
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
process.waitFor();
while ((line = input.readLine()) != null){
out.print(line);
out.print("<br/>");
}
if(input!=null){
input.close();
}
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
本文介绍了一个使用Java执行Shell脚本的具体示例。通过Runtime.getRuntime().exec()方法调用外部Shell脚本并获取其输出,展示了如何读取进程的标准输出流并将输出展示在网页上。

508

被折叠的 条评论
为什么被折叠?



