Anthony
Anthony

Reputation: 195

How to call servlet doPost() method from browser

I'm new to servlet programming and I have written a simple servlet running on Apache Tomcat 9.0.39. The logic in the doPost() and the doGet() methods are different from each other. I can simply invoke the doGet() as follow and receive an accurate response from the servlet:

localhost:8080/myservlet/load?id="4"

The doPost() method is sending two parameters "author" and "title" to the servlet to store. Ultimately, I'd like to keep this simple and not to develop an Html form to submit the values for the "author" and the "title" to the servlet. Is there a way I can invoke the doPost() from the browser as I had for the doGet() method?

Thanks in advance.

Upvotes: 0

Views: 1255

Answers (1)

xohouo
xohouo

Reputation: 58

Actually it does not matter if it is servlet or not. The problem area is regarding Http protocol. From your browser you can send post request, of course.

  1. In you browser, you should have HTML FORM and make the form's method attribute "post" and set the action attribute as url for which you want to post.
  2. Or you could use Javascript fetch api to send post message as ajax. https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

Upvotes: 2

Related Questions