java 如何从jsp中的超链接调用doPost() servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11287642/
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
How to call doPost() servlet from a hyperlink in jsp
提问by faizal
How can I call a servlet
from jsp
? But in this case, I prefer to use doPost()
method than doGet()
.
我怎样才能servlet
从打电话jsp
?但在这种情况下,我更喜欢使用doPost()
method 而不是doGet()
.
this is my code:
这是我的代码:
view.jsp
视图.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.
我打算ViewS
使用链接(编辑 n 删除)从此页面调用 servlet 类(称为)。当我单击编辑并将值存储到数据库中时,我想让特定行中的某些文件可编辑。
and, I want to delete the record in database also record view in jsp when I cleck delete.
并且,当我点击删除时,我想删除数据库中的记录以及jsp中的记录视图。
So please, somebody help me.
所以请有人帮助我。
I've tried to use <a href="/ViewS?action=edit">edit</a>
, but I know this call doGet()
.
我试过使用<a href="/ViewS?action=edit">edit</a>
,但我知道这个电话doGet()
。
Thank you very much for helping me.
非常感谢你帮助我。
采纳答案by Jigar Joshi
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
您需要在单击链接时调用 javascript 函数,并且您需要从 javascript 提交将生成 HTTP POST 的表单
function submitMyForm(){
document.forms["yourFormId"].submit();
}
Or you could make a AJAX call to your servlet
或者您可以对您的 servlet进行AJAX 调用
回答by Dhwaneet Bhatt
AJAX will be the best option for you.
Make an AJAX call from the onClick()
method of the <a>
AJAX 将是您的最佳选择。从 的onClick()
方法进行AJAX 调用<a>