小编典典

从jsp页面执行bash脚本

tomcat

我之前已经编写了一个执行Java类的bash脚本,它在运行该类之前会进行其他检查。运行时需要参数;./SyncIPs.cmd 10.0.1.45并执行一些命令。

我正在尝试使用html / javascript按钮在jsp页面中运行此脚本。到目前为止,我有两个jsp文件,如下所示:

portal.jsp:

      <%
      if(request.getParameter("submitted")==null)
  {

    if(check)
    {

        %><%@include file="Sync.jsp" %><%
    }
    else
    {

        %><%@include file="Sync.jsp" %><%
    }   
 }
     %>

Sync.jsp:

   <%
   if(request.getParameter("submit") == null)
   {

   %>
    <div class=display>this is the Sync</div>

    </div>

    <br>
    <form method="POST" action="/project/jsp/Portal.jsp" id=form2>


  <input type=hidden name=sync>
      <!---this is where I want my script to be run from,--->

   <input type="button" value="" name="submit" action="run" 
   onClick="if(runOnSubmit()){getSomethingWithAjax
   ('Portal.jsp'+getAllFormElementsAndMakeIntoURI(true),
   '','hereIsTheMainHeaderSpan',false,false);}">
   <%
   }//end if
   %>

如何使此脚本运行并发布在portal.jsp页面上?


阅读 605

收藏
2020-06-16

共1个答案

小编典典

您必须在.jsp中放入一些Java代码

try {
  Process p = Runtime.getRuntime().exec("/path_to_your_script/SyncIPs.cmd 10.0.1.45");
  p.waitFor();
  System.out.println("exit code: " + p.exitValue());
} catch (Exception e) {
  System.out.println(e.getMessage());
}

注意:请注意可能的安全问题

2020-06-16