Java Jsp表单Jsp的onClick调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41588024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling method onClick of Jsp form Jsp
提问by abhi314
I wish to call a method in JSP onClick, the method is on the same JSP inside scriptlet.
我希望在 JSP onClick 中调用一个方法,该方法在 scriptlet 中的同一个 JSP 上。
How should I archive this?
我应该如何存档?
<%@ 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>
Note
笔记
The methods name is "sendMail", It's called on submit button I want to do the whole code in JSP only.
方法名称是"sendMail",它在提交按钮上调用我只想在 JSP 中完成整个代码。
采纳答案by abhi314
This is what I ended up doing
这就是我最终做的
<%@ 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;
}
%>
<%
String a = request.getParameter("to");
if(a != null){
sendMail(request.getParameter("to"),request.getParameter("sub"),request.getParameter("msg"));
}
%>
<html>
<head>
<title>Send Email using JSP</title>
</head>
<body><center>
<form action="#" method="post">
<label>Email To</label><br />
<input type="text" name="to" /><br /> <br />
<label>Subject</label><br />
<input type="text" name="sub" /><br /> <br />
<label for="body">Message</label><br />
<input type="text" name="msg" /><br /> <br />
<input type="submit"/>
</form>
</center></body>
</html>
The action="#"
reloads the page, and there is a if
condition which calls the required method if the parameter is not blank( Please keep in mind that by default on first call the parameter will be null ).
在action="#"
重新加载页面,并有if
它调用所需的方法,如果该参数不为空的条件(请记住,默认情况下,在第一次调用的参数将是空)。
回答by Suresh Atta
JSP- Executes on Server.
JSP- 在服务器上执行。
JavaScript - executes in browser.
JavaScript - 在浏览器中执行。
No you cannot call that JSP magically from JS. However you can send an Ajax request or post the form to jsp. BTW, I strongly suggest you to move the java code to a servlet and use it.
不,您不能从 JS 神奇地调用该 JSP。但是,您可以发送 Ajax 请求或将表单发布到 jsp。顺便说一句,我强烈建议您将 java 代码移动到 servlet 并使用它。
回答by Jad Chahine
The
onclick
event occurs when the user clicks on an element. This attribute has the ability to call JS functions (front end)
onclick
当用户单击元素时发生该事件。该属性具有调用JS函数的能力(前端)
In your case, you want to call a JAVA function (server side) so the best way is to move the java code to a servlet and use it.
在您的情况下,您想要调用 JAVA 函数(服务器端),因此最好的方法是将 Java 代码移动到 servlet 并使用它。
Anyway if you want to keep the JAVA function in the jsp, you can do this via ajax in this way
无论如何,如果你想在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
AJAX 是开发者的梦想,因为你可以
无需重新加载页面即可更新网页 从服务器请求数据 - 在页面加载后 从服务器接收数据 - 页面加载后 将数据发送到服务器 - 在后台
Check the full code here
在此处查看完整代码
<%@ 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>
For more information check
欲了解更多信息,请检查