2

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

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

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

just let it be post(..) (it's what he wants)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.