在ssm框架和linux环境下:jsp从页面输入一个IP地址和一个数字,把它们传到home下的shell脚本中,写出完整代码和代码所在的文件名,以及运行结果
时间: 2024-05-07 14:15:22 浏览: 101
用jsp编写计算器,文件供包含连个代码,分别是计算页面代码和显示结果的代码。
1. 首先,在jsp页面中添加输入框,获取IP地址和数字:
```
<form action="/executeShell" method="post">
<label>IP地址:</label>
<input type="text" name="ip"><br/>
<label>数字:</label>
<input type="text" name="num"><br/>
<input type="submit" value="执行">
</form>
```
2. 在Controller中接收参数,并调用shell脚本:
```
@Controller
public class HomeController {
@RequestMapping(value = "/executeShell", method = RequestMethod.POST)
public String executeShell(@RequestParam String ip, @RequestParam String num) {
String command = "sh /home/xxx/test.sh " + ip + " " + num;
try {
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return "result";
}
}
```
3. 在/home/xxx/test.sh中接收参数并执行操作:
```
#!/bin/bash
IP="$1"
NUM="$2"
echo "IP地址:$IP"
echo "数字:$NUM"
```
4. 运行结果会在Controller中输出,并且会在test.sh中打印出IP地址和数字:
```
IP地址:192.168.0.1
数字:123
```
阅读全文