在JSP中,可以使用以下几种方式提交表单:
<form action="submit.jsp" method="get">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
<form action="submit.jsp" method="post">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
submit()
方法提交表单。例如:<form id="myForm" action="submit.jsp" method="post">
<input type="text" name="username">
<input type="button" value="Submit" onclick="submitForm()">
</form>
<script>
function submitForm() {
document.getElementById("myForm").submit();
}
</script>
<form id="myForm">
<input type="text" name="username">
<input type="button" value="Submit" onclick="submitForm()">
</form>
<script>
function submitForm() {
var form = document.getElementById("myForm");
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open("POST", "submit.jsp", true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// 处理服务器响应
}
};
xhr.send(formData);
}
</script>
这些是常见的表单提交方式,在实际应用中可以根据需求选择合适的方式。