如何使用一个提交按钮保存多个表单数据
在我的jsp页面中,我有两种形式.一种形式是进入save_product
servlet,另一种形式是进入save_images
servlet.只有一个提交按钮.首先单击提交按钮时,我要保存产品,然后再保存图像.
In my jsp page I have two forms.one form action goes to save_product
servlet and other one is goes to save_images
servlet. There is only one submit button. when click submit button first I want to save products and then want to save images.
我使用jQuery和Request.getRequestDispatcher(String).forward()
进行了尝试.但未成功.
I tried it using jQuery and Request.getRequestDispatcher(String).forward()
.but it is not succeeded.
这是我的jsp:
<button type="submit" class="btn btn-default" id="formsave1">Save</button>
<div class=container>
<div class="panel">
<form action="../save_product" method="POST"><button type="submit" id="formsave2" ></button>
</div>
<div class="panel">
<form action="../save_images" method="POST" enctype="multipart/form-data">
<button type="submit" id="formsave3"></button>
</div>
</div>
产品保存servlet:
Product save servlet:
req.getRequestDispatcher("save_images").forward(req, resp);
jQuery:
$(document).ready(function () {
$(formsave1).on("click", function (e) {
alert('CLICKED 1');
$('#formsave2').click();
alert('CLICKED 2');
});
您的代码不足以完全识别问题.我无法发现问题.
Your code is insufficient to identify the problem completely. I cannot spot the issue.
但是,根据您的html代码,我猜这两个servlet都在同一个包中.在这种情况下,您可以直接调用save_images
的doPost()
或doGet()
方法,因为它们都具有protected
访问修饰符.
However, as per your html code I guess that both servlets are in the same package. In that case you may directly call the doPost()
or doGet()
methods of save_images
as they both have protected
access modifier.
根本不需要使用Requestdispatcher.
There's no need to use the Requestdispatcher after all.
new save_images().doPost(req, resp);