简体   繁体   English

JSP通过href调用javascript函数

[英]JSP call javascript function through href

The following code in JSP: JSP中的以下代码:

while(rs2.next()) 
    { 
        out.println(rs2.getString(1));
        %>
            <a href="javascript:displayData(<%=rs2.getString(1)%>)">Run The App</a>
        <%
        out.println("<br>");
    }

I want for each element to print its name and a link to JavaScript function that takes the name as a parameter. 我希望每个元素都能打印它的名称,以及一个以名称作为参数的JavaScript函数链接。

The JavaScript that I have is: 我拥有的JavaScript是:

<script type="text/javascript">
    function displayData(ctr) {
        alert(ctr);
        <%
            Process p = null;
            Runtime r = Runtime.getRuntime(); 
            String command; 
            File dir = new File("C:/app/webapps/data"); 
            command="javaw -jar "+ctr;
            try 
            { 
                p = r.exec(command,null,dir);
            }
            catch(Exception e) 
            { } 
        %>
    }
</script>

What the function does is to execute a jar file (the jar file is executed by calling its name which in this case the parameter that I pass ctr ). 该函数的作用是执行一个jar文件(jar文件是通过调用它的名字来执行的,在本例中是我传递ctr的参数)。 The error that I got is: 我得到的错误是:

ctr cannot be resolved to a varialbe

Can someone help me in this problem? 有人可以帮我解决这个问题吗?

The only way to call Jsp from client is to call an url. 从客户端调用Jsp的唯一方法是调用url。 If you want to do this asynchronously (without refreshing) you should use Ajax. 如果要异步执行此操作(无需刷新),则应使用Ajax。 I think you are looking for something like this https://blog.udemy.com/jsp-ajax/ . 我想你正在寻找像这样的东西https://blog.udemy.com/jsp-ajax/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM