Html 单击 JSP 中的 href 发送 post 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6791238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:41:55  来源:igfitidea点击:

Send post request on click of href in JSP

htmljsp

提问by Harry Joy

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 atag? Is it possible?

它将调用该 servlet 的 GET 方法。我可以通过点击a标签发送帖子请求吗?是否可以?

I know how to do this with Javascript but want to know if this could be done without JavaScript.

我知道如何用 Javascript 做到这一点,但想知道这是否可以在没有 JavaScript 的情况下完成。

回答by Michael Allen

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

解决方案是将锚点包围在一个表单中,其中包含 post 方法和您希望执行的操作。在锚点上放一个 javascript 来提交表单

<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.

链接将您带到页面,这是用户理解他们所做的。打破用户的假设并导致 POST 链接,做不可撤销的事情,通常被认为是一个坏主意。

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.

这打破了互联网。

回答by Bozho

Only with javascript: create a <form action="MyServlet">and submit it with form.submit()

仅使用 javascript:创建<form action="MyServlet">并提交form.submit()

You can also send POSTwith ajax (with jQuery: $.post("MyServlet", {param:param}))

您也可以POST使用 ajax发送(使用 jQuery: $.post("MyServlet", {param:param})

But think about the semantics. With POSTyou should postdata. And links are usually simply getting resources. (It's another story if your link is actually a button in disguise)

但想想语义。有了POST你应该张贴数据。而链接通常只是获取资源。(如果你的链接实际上是一个伪装的按钮,那就是另一回事了)

回答by user5892314

Code for Login.jsp page:

Login.jsp 页面的代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
    <form action="LoginServlet" method="post" name="credential">

    Please enter userName : 
    <input type="text" name="un"><br>

    Please enter PassWord :
    <input type="text" name="pw"><br>

    <input type="submit" value="Submit">
    </form>
    <form action="registerUser" name="registerUserForm" method="post">
    If no user name and password then get a new one by <a href="registerUser">clicking</a> here
    </form>
</body>
</html>



code for registerUser servlet::
package examplePackage;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/registerUser")
public class registerUser extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public registerUser() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("registerUser");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}