gpt4 book ai didi

java - Jsp窗体Jsp调用onClick方法

转载 作者:搜寻专家 更新时间:2023-11-01 01:32:06 26 4
gpt4 key购买 nike

我想在 JSP onClick 中调用一个方法,该方法在 scriptlet 中的同一个 JSP 上。

我应该如何存档?

<%@ page import="java.io.*,java.lang.*,java.util.*,java.net.*,java.util.*,java.text.*"%>
<%@ page import="javax.activation.*,javax.mail.*,org.apache.commons.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>


<%!
public String sendMail(String to, String sub, String msg) {
String res = null;
System.out.println("HI");
return res;
}%>

<html>
<head>
<title>Send Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<form>
<label>Email To</label><br />
<input type="text" name="to" /><br />
<label>Subject</label><br />
<input type="text" name="sub" /><br />
<label for="body">Message</label><br />
<input type="text" name="msg" /><br />
<input type="submit" onClick="sendMail( to, sub, msg )"/>
</form>
</body>
</html>


注意事项

方法名称是"sendMail",在提交按钮上调用 我只想在 JSP 中完成整个代码。

最佳答案

The onclick event occurs when the user clicks on an element. This attribute has the ability to call JS functions (front end)

在你的情况下,你想调用一个 JAVA 函数(服务器端),所以最好的方法是将 java 代码移动到一个 servlet 并使用它。

无论如何,如果你想在jsp中保留JAVA函数,你可以通过ajax这样实现

<script type="text/javascript">
$(document).ready(function() {
$('#sendMailBtn').click(function (){
$.ajax({
type: "post",
url: "/path",
data: "email=" + $('#email').val() + "&subject="+$('#subject').val() + "&msg=" + $('#msg').val(),
success: function(msg){
//
}
});
});
});
</script>

AJAX is a developer's dream, because you can

  • Update a web page without reloading the page
  • Request data from a server - after the page has loaded
  • Receive data from a server - after the page has loaded
  • Send data to a server - in the background

  • 查看完整代码

    <%@ page import="java.io.*,java.lang.*,java.util.*,java.net.*,java.util.*,java.text.*"%>
    <%@ page import="javax.activation.*,javax.mail.*,org.apache.commons.*"%>
    <%@ page import="javax.servlet.http.*,javax.servlet.*"%>


    <%!
    public String sendMail(String to, String sub, String msg) {
    String res = null;
    System.out.println("HI");
    return res;
    }
    %>

    <html>
    <head>
    <title>Send Email using JSP</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    </head>

    <body>
    <center>
    <h1>Send Email using JSP</h1>
    </center>
    <form>
    <label>Email To</label><br />
    <input id="email" type="text" name="to" /><br />
    <label>Subject</label><br />
    <input id="subject" type="text" name="sub" /><br />
    <label for="body">Message</label><br />
    <input id="msg" type="text" name="msg" /><br />
    <input id="sendMailBtn" type="submit" />
    </form>
    </body>

    <script type="text/javascript">
    $(document).ready(function() {
    $('#sendMailBtn').click(function (){
    $.ajax({
    type: "post",
    url: "/path",
    data: "email=" + $('#email').val() + "&subject="+$('#subject').val() + "&msg=" + $('#msg').val(),
    success: function(msg){
    //
    }
    });
    });
    });
    </script>
    </html>

    更多信息请查看

  • AJAX简介:http://www.w3schools.com/xml/ajax_intro.asp
  • onclick事件:http://www.w3schools.com/tags/ev_onclick.asp

  • 关于java - Jsp窗体Jsp调用onClick方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41588024/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com