简体   繁体   中英

Can we call Servlet from JSP using JavaScript Function?

I was wondering, Can I call Java Servlet doGet or doPost method from JSP using JavaScript function. If yes, then please help me with Code Snippet. I tried to find it, and searched on Stackoverflow and also on Google... but with hard luck. Any help would be very helpful for me to learn.

Servlet has doGet and doPost and other methods, which will be called http GET and POST respectively.

If you wish to call the servlets you can use the JQuery ajax way of doing it.

Jquery has an ajax function which will take the url and make a call to the server.

//copied from w3schools
$("button").click(function(){
  $.get("demo_test.asp",function(data,status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});

in the place of demo_test.asp you can call your url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM