简体   繁体   English

如何从浏览器调用 servlet doPost() 方法

[英]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.我是 servlet 编程的新手,我编写了一个在 Apache Tomcat 9.0.39 上运行的简单 servlet。 The logic in the doPost() and the doGet() methods are different from each other. doPost()doGet()方法中的逻辑彼此不同。 I can simply invoke the doGet() as follow and receive an accurate response from the servlet:我可以简单地调用doGet()并从 servlet 接收准确的响应:

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

The doPost() method is sending two parameters "author" and "title" to the servlet to store. doPost()方法将两个参数“author”和“title”发送到 servlet 进行存储。 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.最后,我想保持这个简单,而不是开发一个 Html 表单来将"author""title"的值提交给 servlet。 Is there a way I can invoke the doPost() from the browser as I had for the doGet() method?有没有办法像我对doGet()方法那样从浏览器调用doPost()

Thanks in advance.提前致谢。

Actually it does not matter if it is servlet or not.实际上它是否是servlet并不重要。 The problem area is regarding Http protocol.问题区域与 Http 协议有关。 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.在您的浏览器中,您应该拥有 HTML FORM 并使表单的方法属性为“post”,并将 action 属性设置为您要发布的 url。
  2. Or you could use Javascript fetch api to send post message as ajax.或者您可以使用 Javascript fetch api 将帖子消息作为 ajax 发送。 https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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