如何使用javascript将jsp文件中的数据传递给多个servlet?

时间:2022-05-23 21:17:44

I have a jsp file which has a form. And the form has some text fields and image upload field.I need to send text data to one servlet and image to another servlet when I click the submit button. is this possible ?

我有一个带有表单的jsp文件。并且表单有一些文本字段和图像上传字段。当我单击提交按钮时,我需要将文本数据发送到一个servlet并将图像发送到另一个servlet。这可能吗 ?

如何使用javascript将jsp文件中的数据传递给多个servlet?

1 个解决方案

#1


2  

Yes.. I think it is possible .. the way I know you have to use a javascript library like jquery. Below is how it happens

是..我认为有可能..我知道你必须使用像jquery这样的javascript库。以下是它的发生方式

On form submit you prevent the post to servlet. Then you can use ajax like shown below to send 2 requests to 2 different servlets. Below shows one ajax call.. you can do another after that call. I trying to show you below

在表单提交上,您可以阻止发布到servlet。然后你可以使用如下所示的ajax向2个不同的servlet发送2个请求。下面显示了一个ajax调用..在调用之后你可以做另一个。我想告诉你如下

$("form").submit(function(evt){  
      evt.preventDefault();
      var formData = new FormData($(this)[0]);
      var author = $("#author").val();
   $.ajax({
       url: 'fileUploadServletUrl',
       type: 'POST',
       data: formData,
       async: false,
       cache: false,
       contentType: false,
       enctype: 'multipart/form-data',
       processData: false,
       success: function (response) {
         alert(response);
       }
   });

   $.ajax({
       url: 'textDataServletUrl',
       type: 'POST',
       data: {'author':author },
       async: false,
       cache: false,
       processData: false,
       success: function (response) {
         alert(response);
       }
   });

   return false;
 });

#1


2  

Yes.. I think it is possible .. the way I know you have to use a javascript library like jquery. Below is how it happens

是..我认为有可能..我知道你必须使用像jquery这样的javascript库。以下是它的发生方式

On form submit you prevent the post to servlet. Then you can use ajax like shown below to send 2 requests to 2 different servlets. Below shows one ajax call.. you can do another after that call. I trying to show you below

在表单提交上,您可以阻止发布到servlet。然后你可以使用如下所示的ajax向2个不同的servlet发送2个请求。下面显示了一个ajax调用..在调用之后你可以做另一个。我想告诉你如下

$("form").submit(function(evt){  
      evt.preventDefault();
      var formData = new FormData($(this)[0]);
      var author = $("#author").val();
   $.ajax({
       url: 'fileUploadServletUrl',
       type: 'POST',
       data: formData,
       async: false,
       cache: false,
       contentType: false,
       enctype: 'multipart/form-data',
       processData: false,
       success: function (response) {
         alert(response);
       }
   });

   $.ajax({
       url: 'textDataServletUrl',
       type: 'POST',
       data: {'author':author },
       async: false,
       cache: false,
       processData: false,
       success: function (response) {
         alert(response);
       }
   });

   return false;
 });