我有一个 JPS 页面,我想从中执行放置在服务器上的 shell 脚本:下面的代码工作正常,并且“/tmp/Sample.sh”脚本正在服务器上执行。
现在我想做两件事:
1.The script is getting executed as soon as page is loaded, but i want to execute it only when button is clicked.
2.I understand that use of scriplets is discouraged, what I googled is that I should call a servlet, when button is clicked, in which i can move the java code.
我是这些术语的新手,因为我的主要技能不是 java。我已经阅读了 servlet 的理论,但是没有得到我如何才能实现上述两个功能。对实现以上两点的任何帮助将不胜感激
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<%
String unixCommand = "sh /tmp/Sample.sh";
Runtime rt = Runtime.getRuntime();
rt.exec(unixCommand);
%>
</body>
</html>
根据建议更新代码:
http://10.111.24.21:7001/Project_1/Test_1.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<button onclick="location.href = 'http://10.111.24.21:7001/Project_1/JavaServletClass.java';" id="RedirectButton" > Execute</button>
</body>
</html>
http://10.111.24.21:7001/Project_1/JavaServletClass.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class JavaServletClass extends HttpServlet
{
public void init() throws ServletException { }
private void ExampleMethod() throws IOException
{
String unixCommand = "sh /tmp/Sample.sh";
Runtime rt = Runtime.getRuntime();
rt.exec(unixCommand);
}
相关分类