Lalchand
Lalchand

Reputation: 7827

calling Servlet post method using javascript

i need to call servlet post method using javascript ,i have done like this

 var iframe= document.getElementById("iframe");
 iframe.src = "MyServlet";

Upvotes: 2

Views: 3614

Answers (2)

Bozho
Bozho

Reputation: 597016

There are two ways to POST to a server:

  • submit a <form> with method="POST"
  • use ajax and specify POST as method

All other options are invoking GET

That said, MyServlet is unlikely to be a valid path. You need to specify the path that you configured as <url-pattern> in web.xml

Upvotes: 1

SLaks
SLaks

Reputation: 887195

You have done it incorrectly.

You need to set the src to a URL that will invoke the servlet, such as

iframe.src = "/Path/To/Something";

If you want to send a POST request, you'll need to create a <form action="/Path/To/Something" target="IFrameName"> and call submit().


Note that it is more efficient to use AJAX, with an XMLHttpRequest. The easiest way to do that is with jQuery:

$.get("/Path/To/Something");

(Although you would want to call $.post)

Upvotes: 1

Related Questions