JSP Ajax form submit to servlet put method not working





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am having troulbe getting the path parameter out to a servlets doput method via and ajax function on a jsp page.



My jsp page with ajax:



    <form id="user-form">
<table>
<tr>
<td> User id:</td>
<td><input type="text" name="name" id="name"/></td>
</tr>

</table>
<input type="submit" value="Submit"/>
</form>



<script>

var form = $('#user-form');

form.submit(function()
{
$.ajax({
url: 'TestServlet',
data: form.serialize(),
type: 'put',
success: function(data){
console.log(data);
}
});
});


Here is the part of the servlet method that is giving me trouble I haven't put the entire code in to save on clarity.



protected void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//System.out.println(request);
System.out.println(request.getParameter("name"));
String resourceBaseURL = "http://localhost:8080/TEST/webapi/orders/";
String requestedOrder = request.getParameter("name");
URL url;
HttpURLConnection con;
String resultInXml = "";

System.out.println("DoPut Called");
// try to create a connection and request XML format
try {

ObjectFactory objFactory = new ObjectFactory();

Car car = objFactory.createCar();
car.setName(requestedOrder);
url = new URL(resourceBaseURL + requestedOrder);
con = (HttpURLConnection) url.openConnection();
System.out.println(url);


When I submit some data through the form my System.out is confirming that the doput method is being called but the request.getParameter("name") is printing out to null.



Below is a copy of my console print out:



null
DoPut Called
http://localhost:8080/TEST/webapi/orders/null
Put


Null is the value for the form input but should be the name i type in.



I have get and post methods working fine as they are sent to the servelt in the regular way within the form action and method attributes.



Put and delete as far as i know can only be done through ajax or JQuery i just cant seem to figure it out.



I have tested the method out on postman and it is working perfectly.



I am really stuck on this and any help would be greatly appreciated.










share|improve this question































    0















    I am having troulbe getting the path parameter out to a servlets doput method via and ajax function on a jsp page.



    My jsp page with ajax:



        <form id="user-form">
    <table>
    <tr>
    <td> User id:</td>
    <td><input type="text" name="name" id="name"/></td>
    </tr>

    </table>
    <input type="submit" value="Submit"/>
    </form>



    <script>

    var form = $('#user-form');

    form.submit(function()
    {
    $.ajax({
    url: 'TestServlet',
    data: form.serialize(),
    type: 'put',
    success: function(data){
    console.log(data);
    }
    });
    });


    Here is the part of the servlet method that is giving me trouble I haven't put the entire code in to save on clarity.



    protected void doPut(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //System.out.println(request);
    System.out.println(request.getParameter("name"));
    String resourceBaseURL = "http://localhost:8080/TEST/webapi/orders/";
    String requestedOrder = request.getParameter("name");
    URL url;
    HttpURLConnection con;
    String resultInXml = "";

    System.out.println("DoPut Called");
    // try to create a connection and request XML format
    try {

    ObjectFactory objFactory = new ObjectFactory();

    Car car = objFactory.createCar();
    car.setName(requestedOrder);
    url = new URL(resourceBaseURL + requestedOrder);
    con = (HttpURLConnection) url.openConnection();
    System.out.println(url);


    When I submit some data through the form my System.out is confirming that the doput method is being called but the request.getParameter("name") is printing out to null.



    Below is a copy of my console print out:



    null
    DoPut Called
    http://localhost:8080/TEST/webapi/orders/null
    Put


    Null is the value for the form input but should be the name i type in.



    I have get and post methods working fine as they are sent to the servelt in the regular way within the form action and method attributes.



    Put and delete as far as i know can only be done through ajax or JQuery i just cant seem to figure it out.



    I have tested the method out on postman and it is working perfectly.



    I am really stuck on this and any help would be greatly appreciated.










    share|improve this question



























      0












      0








      0








      I am having troulbe getting the path parameter out to a servlets doput method via and ajax function on a jsp page.



      My jsp page with ajax:



          <form id="user-form">
      <table>
      <tr>
      <td> User id:</td>
      <td><input type="text" name="name" id="name"/></td>
      </tr>

      </table>
      <input type="submit" value="Submit"/>
      </form>



      <script>

      var form = $('#user-form');

      form.submit(function()
      {
      $.ajax({
      url: 'TestServlet',
      data: form.serialize(),
      type: 'put',
      success: function(data){
      console.log(data);
      }
      });
      });


      Here is the part of the servlet method that is giving me trouble I haven't put the entire code in to save on clarity.



      protected void doPut(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      //System.out.println(request);
      System.out.println(request.getParameter("name"));
      String resourceBaseURL = "http://localhost:8080/TEST/webapi/orders/";
      String requestedOrder = request.getParameter("name");
      URL url;
      HttpURLConnection con;
      String resultInXml = "";

      System.out.println("DoPut Called");
      // try to create a connection and request XML format
      try {

      ObjectFactory objFactory = new ObjectFactory();

      Car car = objFactory.createCar();
      car.setName(requestedOrder);
      url = new URL(resourceBaseURL + requestedOrder);
      con = (HttpURLConnection) url.openConnection();
      System.out.println(url);


      When I submit some data through the form my System.out is confirming that the doput method is being called but the request.getParameter("name") is printing out to null.



      Below is a copy of my console print out:



      null
      DoPut Called
      http://localhost:8080/TEST/webapi/orders/null
      Put


      Null is the value for the form input but should be the name i type in.



      I have get and post methods working fine as they are sent to the servelt in the regular way within the form action and method attributes.



      Put and delete as far as i know can only be done through ajax or JQuery i just cant seem to figure it out.



      I have tested the method out on postman and it is working perfectly.



      I am really stuck on this and any help would be greatly appreciated.










      share|improve this question
















      I am having troulbe getting the path parameter out to a servlets doput method via and ajax function on a jsp page.



      My jsp page with ajax:



          <form id="user-form">
      <table>
      <tr>
      <td> User id:</td>
      <td><input type="text" name="name" id="name"/></td>
      </tr>

      </table>
      <input type="submit" value="Submit"/>
      </form>



      <script>

      var form = $('#user-form');

      form.submit(function()
      {
      $.ajax({
      url: 'TestServlet',
      data: form.serialize(),
      type: 'put',
      success: function(data){
      console.log(data);
      }
      });
      });


      Here is the part of the servlet method that is giving me trouble I haven't put the entire code in to save on clarity.



      protected void doPut(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      //System.out.println(request);
      System.out.println(request.getParameter("name"));
      String resourceBaseURL = "http://localhost:8080/TEST/webapi/orders/";
      String requestedOrder = request.getParameter("name");
      URL url;
      HttpURLConnection con;
      String resultInXml = "";

      System.out.println("DoPut Called");
      // try to create a connection and request XML format
      try {

      ObjectFactory objFactory = new ObjectFactory();

      Car car = objFactory.createCar();
      car.setName(requestedOrder);
      url = new URL(resourceBaseURL + requestedOrder);
      con = (HttpURLConnection) url.openConnection();
      System.out.println(url);


      When I submit some data through the form my System.out is confirming that the doput method is being called but the request.getParameter("name") is printing out to null.



      Below is a copy of my console print out:



      null
      DoPut Called
      http://localhost:8080/TEST/webapi/orders/null
      Put


      Null is the value for the form input but should be the name i type in.



      I have get and post methods working fine as they are sent to the servelt in the regular way within the form action and method attributes.



      Put and delete as far as i know can only be done through ajax or JQuery i just cant seem to figure it out.



      I have tested the method out on postman and it is working perfectly.



      I am really stuck on this and any help would be greatly appreciated.







      jquery ajax jsp servlets xmlhttprequest






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 23:46







      user2963022

















      asked Nov 16 '18 at 21:59









      user2963022user2963022

      65111




      65111
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I'm wondering whether your form is actually loaded in DOM at the time you declare the "form" variable. Try wrapping your jquery code in DOM ready to ensure all elements of the page are ready:



          $(document).ready(function() {

          var $form = $('#user-form');

          $form.submit(function(event) {

          event.preventDefault();

          var data = $form.serialize();
          console.log(data);

          $.ajax({
          ...
          });

          });
          }





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345994%2fjsp-ajax-form-submit-to-servlet-put-method-not-working%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I'm wondering whether your form is actually loaded in DOM at the time you declare the "form" variable. Try wrapping your jquery code in DOM ready to ensure all elements of the page are ready:



            $(document).ready(function() {

            var $form = $('#user-form');

            $form.submit(function(event) {

            event.preventDefault();

            var data = $form.serialize();
            console.log(data);

            $.ajax({
            ...
            });

            });
            }





            share|improve this answer




























              0














              I'm wondering whether your form is actually loaded in DOM at the time you declare the "form" variable. Try wrapping your jquery code in DOM ready to ensure all elements of the page are ready:



              $(document).ready(function() {

              var $form = $('#user-form');

              $form.submit(function(event) {

              event.preventDefault();

              var data = $form.serialize();
              console.log(data);

              $.ajax({
              ...
              });

              });
              }





              share|improve this answer


























                0












                0








                0







                I'm wondering whether your form is actually loaded in DOM at the time you declare the "form" variable. Try wrapping your jquery code in DOM ready to ensure all elements of the page are ready:



                $(document).ready(function() {

                var $form = $('#user-form');

                $form.submit(function(event) {

                event.preventDefault();

                var data = $form.serialize();
                console.log(data);

                $.ajax({
                ...
                });

                });
                }





                share|improve this answer













                I'm wondering whether your form is actually loaded in DOM at the time you declare the "form" variable. Try wrapping your jquery code in DOM ready to ensure all elements of the page are ready:



                $(document).ready(function() {

                var $form = $('#user-form');

                $form.submit(function(event) {

                event.preventDefault();

                var data = $form.serialize();
                console.log(data);

                $.ajax({
                ...
                });

                });
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 23:48









                Eric VautierEric Vautier

                1043




                1043
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345994%2fjsp-ajax-form-submit-to-servlet-put-method-not-working%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python