在JSP中点击href发送帖子请求 [英] Send post request on click of href in JSP
问题描述
如果我们写下如下内容:
< a href =MyServlet>链接< / a>
它会调用该servlet的GET方法。我可以通过点击 a
标签发送发布请求吗?是否有可能?
我知道如何用Javascript做到这一点,但想知道如果没有JavaScript可以完成。
解决方案是将锚定在一个表单中,其中包含post方法和您希望执行的操作。在锚上放一个javascript来提交表单
< form name =submitFormmethod =POSTaction = / servlet的/ ServletName>
< input type =hiddenname =param1value =param1Value>
< A HREF =javascript:document.submitForm.submit()>点击我< / A>
< / form>
编辑
我想我应该提一下,这不是一个好主意。
链接带你到页面,这就是用户了解他们做的事情。打破用户的假设,并导致链接到POST,做一个不可撤销的事情,通常被认为是一个坏主意。
使用一个按钮,它的语义标签,然后你的用户知道点击这个做了一些事情。
秒编辑
我真的需要强调,这根本不是一个好主意。。
这打破了互联网。
If we write something as follow:
<a href="MyServlet">Link</a>
It will call GET method of that servlet. Can I send post request on click of a
tag? Is it possible?
I know how to do this with Javascript but want to know if this could be done without JavaScript.
The solution is to surround the anchor in a form, which has the post method and the action you wish to execute. On the anchor put a javascript to submit the form
<form name="submitForm" method="POST" action="/servlet/ServletName">
<input type="hidden" name="param1" value="param1Value">
<A HREF="javascript:document.submitForm.submit()">Click Me</A>
</form>
edit
I think I should mention that this isn't a good idea.
Links take you to pages, that's what users understand them to do. To break the users assumptions and cause a link to POST, to do an irrevocable thing, is generally considered a bad idea.
Use a button, label it semantically, then your user knows that clicking this does something.
second edit
I really need to emphasise that this isn't a good idea at all.
This breaks the internet.
这篇关于在JSP中点击href发送帖子请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!