运行时错误
我是 jsp 和运行时的新手。我需要能够使用 Web 应用程序页面上的按钮运行脚本。该脚本的位置位于 /home/user/ejbca_4/bin 并运行它,
cd ejbca_4/bin
./ejbca.sh batch
有人建议将这些命令放入 /home/user/bin 中的 .sh 文件中。我使用以下内容创建了 /home/user/bin/test.sh:
#!/bin/bash
cd ejbca_4/bin
./ejbca.sh batch
我将其添加到 jsp 页面,
<input type="submit" name="Generate test" value='<c:out value="Generate Test"/>' tabindex="<%=tabindex++%>"
onClick='generate()'>
function generate() {
Process proc = Runtime.getRuntime().exec("test.sh",null,null);
}
但不知何故这给出了 NumberFormatException。我删除了 Process proc:
function generate() {
Runtime.getRuntime().exec("test.sh",null,null);
}
并且没有 NumberFormatException。但是,该脚本并未运行。谁能解释一下应该如何做?
I am new to jsp and runtime. I need to be able to run a script with a button on a web application page. The location of the script is in /home/user/ejbca_4/bin and to run it,
cd ejbca_4/bin
./ejbca.sh batch
Someone recommended to put these commands in a .sh file in /home/user/bin . I created /home/user/bin/test.sh with the following contents:
#!/bin/bash
cd ejbca_4/bin
./ejbca.sh batch
I added this to the jsp page
<input type="submit" name="Generate test" value='<c:out value="Generate Test"/>' tabindex="<%=tabindex++%>"
onClick='generate()'>
function generate() {
Process proc = Runtime.getRuntime().exec("test.sh",null,null);
}
but somehow this gave a NumberFormatException. I removed Process proc:
function generate() {
Runtime.getRuntime().exec("test.sh",null,null);
}
and there was no NumberFormatException. However, the the script was not run. Can anyone explain how this should be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要了解代码的哪一部分在服务器上运行,哪一部分在客户端(浏览器)上运行。并且您永远不应该将应在服务器上运行的代码放入客户端,因为这显然不起作用。
在您的情况下,应该在服务器上运行的代码是:
它应该被
<% %>
包围并且客户端代码是:
You need to understand which part of your code is running on the server and which part is running on the client(browser). And you should never put code that should be run on the server into the client side, because that won't work(obviously).
In your case, code that should be run on the server is:
It should be surrounded by
<% %>
And the client side code is:
你的
函数generate()
应该是什么?我没有看到它出现在任何类型的标签中,但当您尝试调用 Java 代码时,它看起来像 JavaScript。现在,您的
onClick
将在客户端处理,并且需要一种方法来调用服务器端的运行时调用。目前,客户没有办法实现这一点。您需要执行某种回调(可能使用 AJAX),这将导致服务器执行您的运行时调用。如果它保留在 JSP 中,则需要将其括在<% ... %>
中,以将其计算为 Java 代码。由于您是 JSP 新手,我强烈建议您阅读 Oracle 的 Web Tier 教程,网址为 http://docs.oracle.com/javaee/5/tutorial/doc/bnadp.html。
What is your
function generate()
supposed to be? I don't see that it's in any sort of tags, but it looks like JavaScript - when you're trying to invoke Java code.Right now, your
onClick
will be handled client-side, and it needs a way to invoke your Runtime calls on the server-side. Right now, the client doesn't have a path to make this happen. You need to do some sort of callback (possibly using AJAX) that will result in the server executing your Runtime call. If it remains in a JSP, you need it surrounded in<% ... %>
to have it evaluated as Java code.As you're new to JSPs, I'd highly recommend you go through Oracle's Web Tier tutorial at http://docs.oracle.com/javaee/5/tutorial/doc/bnadp.html .