Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...