java - What is the best way to communicate between a jsp page and a Servlet? -


i'm making dynamic web project in eclipse , cannot figure out how send request user (via button click) servlet perform operation (including database lookup) , populate webpage formatted results. have database lookup functions created. best way this? need 1 string passed servlet, "category" of books wish return arraylist. sources seem indicate jsp page should not used relaying information servlet confused.

there several ways this:

  1. form submit

    <form action="/myservlet" method="post">     <input type="text" name="category" id="category"/>     <input type="submit" value="submit" id="btnsubmit"/> 

    and in servlet code (dopost()):

    string category = request.getparameter("category"); 
  2. using ajax (jquery ajax cleaner)

    $.ajax({     method: "post",     url: "/myservlet",     data: { category: $("#category").val()} //post category field }).done(function( msg ) {     alert( msg ); //alert html returned servlet  }); 
  3. jquery ajax (get)

    $("btnsubmit").click(function(event){     event.preventdefault();     $.get("/myservlet", function(data, status){         alert("data: " + data + "\nstatus: " + status);     });  }); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -