0

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.

Pallav Raj
  • 1,684
  • 3
  • 17
  • 29
  • for call servlet you should send GET or POST request to server from script – Cuzz Jan 18 '15 at 16:44
  • JavaScript is client side code. You could try to create request for resource created by servlet with [AJAX](http://en.wikipedia.org/wiki/Ajax_(programming)). – Pshemo Jan 18 '15 at 16:44
  • take one simple `form` submit example through `javascript`, Method param should be `servlet` path. that will help you. – bNd Jan 18 '15 at 16:46
  • Yes, I know that JavaScript is client side code :).
    I can call my Java Servlet like this, but i wanted to ask how to send GET or POST request to server from JavaScript?
    – Pallav Raj Jan 18 '15 at 16:48
  • 1
    http://stackoverflow.com/questions/958040/what-is-ajax-really, https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started, http://api.jquery.com/jquery.ajax/ – Pavel Horal Jan 18 '15 at 16:49
  • Thx @PavelHoral, we will have to use AJAX for calling POST or GET mothod... Is there not any way other than this?? – Pallav Raj Jan 18 '15 at 16:55
  • 2
    Well you want to make request from JS to server.... that is what AJAX is primarily there for. Of course you can submit forms into IFRAMEs, add dynamic ` – Pavel Horal Jan 18 '15 at 16:59

1 Answers1

0

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