通过提交表单来调用jsp函数



我想在单击提交按钮时执行一个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库(。

这就是如何从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");     
}

示例来自:如何使用Servlet和Ajax

onClick((函数用于JavaScript或JavaScript框架,您可以用JS做您想做的事情。

在JS中创建一个带有参数的函数,然后,onClick((将看起来像这个

onClick="(myFunction(variableName((">

myFunction(variableName(内部应该像这个

myFunction(variableName){ alert("The variable is: " +variableName); }

alert("变量为:"+variableName(;将在网页顶部创建一个弹出窗口,显示您的变量值。

您可以尝试使用以下代码:

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"/>

来源:使用scriptlet 的onClick函数调用

最新更新