单击提交按钮时,我想执行jsp函数。这是我想要做的:
<form>
<input type="text" id="input">
<input type="submit" onClick="(I want the function to be here)">
</form>
<@!
public static void get(){
String s = request.getParameter("input");
System.out.println(s);
}
(我简化了代码,如果您不明白,请告诉我。)
我真的不知道这是否有可能,我一直在搜索,也看到了关于使用AJAX的类似案例的建议,但是我并不熟悉,如果有一个更简单的解决方案,它将是非常容易。
编辑:我只是想指定我要调用的函数不是那么简单,我简化了。实际函数使用只有Java才能运行的代码(使用Java库)。
答案 0 :(得分:1)
这是如何从jsp文件向一些Java代码发出简单的ajax请求:
以纯文本格式返回字符串
jsp页面:
<!DOCTYPE html>
<html lang="en">
<head>
<title>SO question 4112686</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).on("click", "#somebutton", function() { // When HTML DOM "click" event is invoked on element with ID "somebutton", execute the following function...
$.get("someservlet", function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
$("#somediv").text(responseText); // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
});
});
</script>
</head>
<body>
<button id="somebutton">press here</button>
<div id="somediv"></div>
</body>
</html>
Servlet doGet()方法:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String text = "some text";
//you can write whatever java code you want to have here, and you can pass the results to the jsp file through the response writer. (text) This will only work from simple strings. If you want to pass more complicated information like lists or objects you will need to convert it to a json object
response.setContentType("text/plain"); // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
response.getWriter().write(text); // Write response body.
}
如果您想要以某种形式进行操作,则可以这样做:
取消现有表单
<form id="someform" action="someservlet" method="post">
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="text" name="baz" />
<input type="submit" name="submit" value="Submit" />
</form>
<script>
$(document).on("submit", "#someform", function(event) {
var $form = $(this);
$.post($form.attr("action"), $form.serialize(), function(response) {
// ...
});
event.preventDefault(); // Important! Prevents submitting the form.
});
</script>
Servlet:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String foo = request.getParameter("foo");
String bar = request.getParameter("bar");
String baz = request.getParameter("baz");
//do some java stuff here
//then return whatever you want
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8"); /
response.getWriter().write("hello world");
}
答案 1 :(得分:0)
onClick()函数旨在用于JavaScript或JavaScript框架,您可以使用JS进行尝试。
在JS中创建一个带有参数的函数,然后onClick()将如下所示
onClick =“(myFunction(variableName))”
内部myFunction(variableName)应该看起来像这样
myFunction(variableName){
alert("The variable is: " +variableName);
}
alert(“变量为:” + variableName);会像在网页顶部弹出一样显示您的变量值。
答案 2 :(得分:0)
您可以尝试使用以下代码:
if(request.getParameter("btnSubmit")!=null) //btnSubmit is the name of your button, not id of that button.
{
java.util.Date d = new java.util.Date();
System.out.println(d.toString());
}
<input type="submit" id="btnSubmit" name="btnSubmit" value="Execute Test"/>
来源:{{3}}