运行时错误

发布于 2024-12-20 06:36:02 字数 812 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放飞的风筝 2024-12-27 06:36:02

您需要了解代码的哪一部分在服务器上运行,哪一部分在客户端(浏览器)上运行。并且您永远不应该将应在服务器上运行的代码放入客户端,因为这显然不起作用。

在您的情况下,应该在服务器上运行的代码是:

Runtime.getRuntime().exec("test.sh", null, null);

它应该被 <% %> 包围

并且客户端代码是:

<input ... onclick='generate()'>
<script>
    function generate() {
        // tells the server to do 'Runtime.getRuntime().exec(...)'
    }
</script>

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:

Runtime.getRuntime().exec("test.sh", null, null);

It should be surrounded by <% %>

And the client side code is:

<input ... onclick='generate()'>
<script>
    function generate() {
        // tells the server to do 'Runtime.getRuntime().exec(...)'
    }
</script>
回忆躺在深渊里 2024-12-27 06:36:02

你的函数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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文