Search code examples
jqueryhtmljspservletsrequestdispatcher

How to save multiple form data using one submit button


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.

I tried it using jQuery and Request.getRequestDispatcher(String).forward() .but it is not succeeded.

This is my 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>

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');
                });

Solution

  • Your code is insufficient to identify the problem completely. I cannot spot the issue.

    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.

    There's no need to use the Requestdispatcher after all.

    new save_images().doPost(req, resp);