简体   繁体   中英

How to call doPost() servlet from a hyperlink in jsp

How can I call a servlet from jsp ? But in this case, I prefer to use doPost() method than doGet() .

this is my code:

view.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" import="DSIP.*" import="java.util.ArrayList" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>DSIP.View</title>
</head>

<body>
<jsp:useBean id="ipList" scope="application" class="DSIP.IPBeanMapper"/>
<jsp:useBean id="bean" scope="application" class="DSIP.IPBean"/>
<form name="form1" method="post" action="viewS">
    <table width="" border="">
        <tr bgcolor="#0099FF">
            <td width="90"><div align="center">ip</div></td>
            <td width="90"><div align="center">username</div></td>
            <td width="90"><div align="center">password</div></td>
            <td width="90"><div align="center">maxRetry</div></td>
            <td width="90"><div align="center">action</div></td>
        </tr>
        <%
            ArrayList<IPBean> list;
            list = ipList.getIPList();
            for (int i = 0; i < list.size(); i++){
                bean = list.get(i);
        %>
        <tr>
            <td><input name="ip"        type="text" size="15" value="<%=list.get(i).getIp()%>"></td>
            <td><input name="userName"  type="text" size="15" value="<%=bean.getUserName()%>"></td>
            <td><input name="password"  type="text" size="15" value="<%=bean.getPassword()%>"></td>
            <td><input name="maxRetry"  type="text" size="15" value="<%=bean.getMaxRetry()%>"></td>
            <td><a href="/ViewS?action=edit">edit</a> <a href="/ViewS?action=delete">delete</a>

            </td>
        </tr>
        <%
            }
        %>
    </table>
    <input type="submit" name="Submit" value="Submit">

</form>
</body>
</html>

I intend to call a servlet class (called ViewS ) from this page using link (edit n delete). I want to make some filed in a specific row being editable when I click edit and store the values into a database.

and, I want to delete the record in database also record view in jsp when I cleck delete.

So please, somebody help me.

I've tried to use <a href="/ViewS?action=edit">edit</a> , but I know this call doGet() .

Thank you very much for helping me.

You need to call a javascript function on click of link and from javascript you need to submit the form that will generate a HTTP POST

function submitMyForm(){
 document.forms["yourFormId"].submit();
}

Or you could make a AJAX call to your servlet

AJAX will be the best option for you. Make an AJAX call from the onClick() method of the <a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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