简体   繁体   English

使用HTML在JSP中调用Java scriptlet

[英]invoking java scriptlet in JSP using HTML

I am trying to find a way to invoke a piece of java code within the JSP using HTML form 我正在尝试找到一种使用HTML表单在JSP中调用一段java code的方法

  <form method="get" action="invokeMe()">

       <input type="submit" value="click to submit" />

  </form>


  <%
     private void invokeMe(){
       out.println("He invoked me. I am happy!");   
     }
  %>

the above code is within the JSP. 上面的代码在JSP中。 I want this run the scriptlet upon submit 我希望此脚本在提交时运行

I know the code looks very bad, but I just want to grasp the concept... and how to go about it. 我知道代码看起来很糟糕,但是我只想掌握概念...以及如何实现它。

thanks 谢谢

You can use Ajax to submit form to servlet and evaluate java code, but stay on the same window. 您可以使用Ajax将表单提交到Servlet并评估Java代码,但是要保留在同一窗口中。

<form method="get" action="invokeMe()" id="submit">

       <input type="submit" value="click to submit" />

</form>

<script>
    $(document).ready(function() {
        $("#submit").submit(function(event) {
            $.ajax({
                type : "POST",
                url : "your servlet here(for example: DeleteUser)",
                data : "id=" + id,
                success : function() {
                    alert("message");
                }
            });
            $('#submit').submit(); // if you want to submit form
        });
    });
</script>

Sorry, not possible. 抱歉, 不可能。

Jsp lies on server side and html plays on client side unless without making a request you cannot do this :) Jsp位于server端,而html位于client side除非没有发出request否则您不能这样做:)

you cannot write a java method in scriptlet . 您不能在scriptlet编写java method Because at compilation time code in scriptlet becomes part of service method. 因为在compilation时, scriptlet代码成为service方法的一部分。 Hence method within a method is wrong. 因此,方法中的方法是错误的。

How ever you can write java methods within init tag and can call from scriptlet like below code. 您如何在init tag内编写java methods ,以及如何从scriptlet调用,如以下代码所示。

<form method="get" action="">

       <input type="submit" value="click to submit" />

  </form>

    <%
        invokeMe();

     %>

  <%!
       private void invokeMe(){
       out.println("He invoked me. I am happy!");   
     }
   %>

Not possible. 不可能。 When the form is submitted, it sends a request to the server. 提交表单后,它将向服务器发送请求。 You have 2 options: 您有2个选择:

Have the server perform the desired action when the it receives the request sent by the form 当服务器收到表单发送的请求时,让服务器执行所需的操作

or 要么

Use Javascript to perform the desired action on the client: 使用Javascript在客户端上执行所需的操作:

 <form name="frm1" action="submit" onsubmit="invokeMe()"
 ...
 </form>

 <script>
 function invokeMe()
 {
 alert("He invoked me. I am happy!")
 }
 </script>

You can't do this since JSP rendering happens on server-side and client would never receive the Java code (ie. the invokeMe() function) in the returned HTML. 您无法执行此操作,因为JSP呈现发生在服务器端,并且客户端将永远不会在返回的HTML中接收Java代码(即invokeMe()函数)。 It wouldn't know what to do with Java code at runtime, anyway! 无论如何,它都不知道在运行时如何处理Java代码!

What's more, <form> tag doesn't invoke functions , it sends an HTTP form to the URL specified in action attribute. 此外, <form>标记不会调用函数 ,它会将HTTP form发送到action属性中指定的URL。

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

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