运行环境:1.linux系统 2. ssm框架 3. tomcat9.0 4. jdk1.8 要求:jsp从页面输入一个IP地址和一个数字,把它们传到home下的shell脚本中,写出完整代码和代码所在的文件名,以及运行结果
时间: 2024-06-11 16:10:21 浏览: 104
文件名:ip.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>IP地址输入</title>
</head>
<body>
<form action="runShell.jsp" method="post">
IP地址:<input type="text" name="ip"><br>
数字:<input type="text" name="num"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
文件名:runShell.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>执行shell脚本</title>
</head>
<body>
<%
String ip = request.getParameter("ip");
String num = request.getParameter("num");
if(StringUtils.isEmpty(ip) || StringUtils.isEmpty(num)){
out.println("参数不完整");
}else{
String command = "/home/run.sh " + ip + " " + num;
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while((line = reader.readLine()) != null){
out.println(line+"<br>");
}
} catch (IOException e) {
e.printStackTrace();
}
}
%>
</body>
</html>
```
文件名:run.sh
```bash
#!/bin/bash
ip=$1
num=$2
echo "IP地址为:$ip"
echo "数字为:$num"
```
运行结果:在浏览器中访问ip.jsp页面,输入IP地址和数字,点击提交按钮,会跳转到runShell.jsp页面,页面会输出IP地址和数字。同时,shell脚本也会输出IP地址和数字。
阅读全文