如何在不使用表单的情况下从jsp调用servlet
这可能是一个重复的问题,我很抱歉。我有一个 jsp 页面,其中有一些按钮。每个按钮都有自己的 servlet 来调用。我想知道是否有任何方法可以在不使用表单的情况下调用这些 servlet,因为用户可以选择给定的 3 个功能中的任何一个。我还需要将一个值从 jsp 页面传递到我调用的 servlet。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Configurations</title>
<script type="text/javascript">
function runConfiguration(){
var config=${dataValues.get(0)};
//call servlet
}
function editConfiguration(){
var config=${dataValues.get(0)};
//call servlet
}
function deleteConfiguration(){
var config=${dataValues.get(0)};
//call servlet
}
</script>
</head>
<body>
<%
String[] label={"Master Port","Baud Rate","Char Size","Stop Bits","Parity","RTU Port","Baud Rate","Char Size","Stop Bits", "Parity"};
int i=0;
%>
<br>
<br>
<br>
<table align="center" border="1">
<td><div align="center" style="background-color: goldenrod;"><b> ${dataValues.get(0)}</b></div>
<table width="210" align="left" border="1">
<td bgcolor="goldenrod"><b> Header1 </b></td>
<c:forEach var="data" begin="1" end="5" items="${dataValues}" varStatus="status">
<tr>
<td><%=label[i++]%>: ${data}</td>
</tr>
</c:forEach>
</table>
<table width="210" align="left" border="1">
<td bgcolor="goldenrod"><b> Header2 </b></td>
<c:forEach var="data" begin="6" end="10" items="${dataValues}" varStatus="status">
<tr>
<td><%=label[i++]%>: ${data}</td>
</tr>
</c:forEach>
</table>
</td>
</table>
<c:choose>
<c:when test="${dataValues.get(11)==1}">
<p align="center"><b><i>This configuration is already running</i></b></p>
<p align="center">
<input type="button" value="stop" onclick="StopConfiguration"/>
</p>
</c:when>
<c:otherwise>
<p align="center"><b><i>This configuration is currently NOT running</i></b></p>
<p align="center">
<button type="button" onclick="runConfiguration()">Run</button>
<button type="button" onclick="editConfiguration()">Edit</button>
<button type="button" onclick="deleteConfiguration()">Delete</button>
</p>
</c:otherwise>
</c:choose>
</body>
</html>
This could be a repeat question, I apologize. I have a jsp page in which I have some buttons. Each button has its own servlet to call. I want to know if there is any way I can call these servlets without using form because the user may choose any of the 3 functionalities given. I also need to pass a value from the jsp page to the servlets I call.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Configurations</title>
<script type="text/javascript">
function runConfiguration(){
var config=${dataValues.get(0)};
//call servlet
}
function editConfiguration(){
var config=${dataValues.get(0)};
//call servlet
}
function deleteConfiguration(){
var config=${dataValues.get(0)};
//call servlet
}
</script>
</head>
<body>
<%
String[] label={"Master Port","Baud Rate","Char Size","Stop Bits","Parity","RTU Port","Baud Rate","Char Size","Stop Bits", "Parity"};
int i=0;
%>
<br>
<br>
<br>
<table align="center" border="1">
<td><div align="center" style="background-color: goldenrod;"><b> ${dataValues.get(0)}</b></div>
<table width="210" align="left" border="1">
<td bgcolor="goldenrod"><b> Header1 </b></td>
<c:forEach var="data" begin="1" end="5" items="${dataValues}" varStatus="status">
<tr>
<td><%=label[i++]%>: ${data}</td>
</tr>
</c:forEach>
</table>
<table width="210" align="left" border="1">
<td bgcolor="goldenrod"><b> Header2 </b></td>
<c:forEach var="data" begin="6" end="10" items="${dataValues}" varStatus="status">
<tr>
<td><%=label[i++]%>: ${data}</td>
</tr>
</c:forEach>
</table>
</td>
</table>
<c:choose>
<c:when test="${dataValues.get(11)==1}">
<p align="center"><b><i>This configuration is already running</i></b></p>
<p align="center">
<input type="button" value="stop" onclick="StopConfiguration"/>
</p>
</c:when>
<c:otherwise>
<p align="center"><b><i>This configuration is currently NOT running</i></b></p>
<p align="center">
<button type="button" onclick="runConfiguration()">Run</button>
<button type="button" onclick="editConfiguration()">Edit</button>
<button type="button" onclick="deleteConfiguration()">Delete</button>
</p>
</c:otherwise>
</c:choose>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在这里看到 2 个选项:
在发送 POST 请求之前,使用 JavaScript 根据每个按钮更改表单的 URL;
对所有 3 种情况使用表单和相同的 servlet。在 servlet 中,您应该确定按下了哪个按钮(它们的值作为请求参数传递),然后相应地继续。
I see 2 options here:
Changing a form's URL accordingly to each button using JavaScript, right before sendiing a POST request;
Use a form and the same servlet for all 3 cases. In the servlet you should determine what button has been pressed (their values are passed as a request parameter) and then go forward accordingly.
您可以使用 Ajax 来执行此操作,或者使用更糟糕但可运行的方式来执行此操作,您可以更改当前窗口 url 并重新加载页面。
对于 Ajax 调用,我建议您使用 ExtJS。 请参见 Ext.Ajax 用法
其他方法 :更改 href, 重新加载窗口
You can use Ajax to do this or a worse but runnable way, you can change the current window url and reload the page.
For Ajax call I recommend for you to use ExtJS. See Ext.Ajax Usage
For the other ways: change the href, reload the window
您还可以使用 Javascript,当用户点击按钮时调用脚本并动态更改 url 并提交该表单。
这是一个示例......
并且在您的脚本中
You can also use Javascript and at the time when user hit a button call the script and dynamically change the url and submit that form.
Here is one Example.....
And in Your Script
请在 JSP 和 JSP 中指定操作字符串。在您的 servlet 中检查它。
根据该字符串操作,您可以在 servlet 代码中设置条件,以确定您希望该特定操作执行什么操作。
请参阅下面,这是我在代码中所做的。
例如
,您可以只调用一个 servlet,而不是调用许多 servlet。
在您的 servlet 中使用这样的代码。
Please specify the action string in your JSP & check for it in your servlet.
Depending on that string action you can set the condition in your servlet code, as to what action you want that particular action to perform.
Please see below this is what i have done in my code.
FOR EXAMPLE
Instead of calling many servlets, you can just call one servlet.
Inside your servlet use code like this.
你可以使用这个:
如果你想添加一些引导程序,你所要做的就是
You can use this :
If you wanna add some bootstrap to it, all you have to do is