Harry Joy
Harry Joy

Reputation: 59660

Send post request on click of href in JSP

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.

Upvotes: 12

Views: 63739

Answers (3)

user5892314
user5892314

Reputation: 1

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);
    }
}

Upvotes: 0

Michael Allen
Michael Allen

Reputation: 5828

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.

Upvotes: 26

Bozho
Bozho

Reputation: 597046

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)

Upvotes: 0

Related Questions