0

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');
                });
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Poorna Senani Gamage
  • 1,246
  • 2
  • 19
  • 30
  • 1
    you can't forward the request like that. If you want to "forward" the request you need to use `response.sendRedirect`. But you will not be able to use this with POST because it won't retain the parameters... Why not just fix your back end so you only need to call 1 servlet? – Jonathan Laliberte Jun 02 '18 at 20:44
  • 1
    @JonathanLaliberte That's not true, you can forward the request to another servlet including all the POST or GET parameters. – Roshana Pitigala Jun 03 '18 at 05:30
  • 1
    Your code doesn't seem to have any problem, or the given data is insufficient to provide an answer. Keep in mind that if the date is sent via POST `doPost()` will be called in both servlets. – Roshana Pitigala Jun 03 '18 at 05:33
  • thanx both of you. req.getRequestDispatcher("save_images").forward(req, resp); is correct. it access save_image servlet. – Poorna Senani Gamage Jun 03 '18 at 06:23
  • 1
    Ah interesting. Good to know. Thanks for the correction Roshana. – Jonathan Laliberte Jun 03 '18 at 08:34

1 Answers1

2

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);
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80