11

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Harry Joy
  • 58,650
  • 30
  • 162
  • 207

3 Answers3

26

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.

Michael Allen
  • 5,712
  • 3
  • 38
  • 63
  • Nice! Don't forget create the javascript method that will be called by HREF – deldev Mar 22 '14 at 20:01
  • @dellasavia the javascript method being called `document.submitForm.submit()` is actually the submit function attached to the form named submitForm. It's created for you by the DOM. – Michael Allen Jun 20 '14 at 10:00
  • In security scans Instead of GET it looks for POST for that scenario I think its a good idea – Juke Jun 06 '19 at 15:20
  • But this one on opening link in new tab blocks the pae from opening is there any other alternative? – Juke Jun 06 '19 at 17:00
0

Code for Login.jsp page:

<%@ 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);
    }
}
Qirel
  • 25,449
  • 7
  • 45
  • 62
0

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

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

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140