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:
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");
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 });
jquery ajax (get)
$("btnsubmit").click(function(event){ event.preventdefault(); $.get("/myservlet", function(data, status){ alert("data: " + data + "\nstatus: " + status); }); });
Comments
Post a Comment