javascript 单击html表单的不同按钮时如何调用不同的servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26561727/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:09:10  来源:igfitidea点击:

how to call different servlet on clicking different button of html form

javascripthtmlservlets

提问by devendrak353

i have a html form i want to call two different servlet on clicking two different button how to change the form action run time

我有一个 html 表单,我想在单击两个不同的按钮时调用两个不同的 servlet 如何更改表单操作运行时间

<form>
<input type="submit" value="Question Paper" style="height:25px; width:120px; background-color: royalblue;color: white;" />
<input type="submit" value="Instruction" style="height:25px; width:120px; background-color: royalblue;color: white;" />");
 </form>

on clicking the button i want to call servlet1 and servlet2 on each button

单击按钮时,我想在每个按钮上调用 servlet1 和 servlet2

please help me to solve this problem, thanks in advance

请帮我解决这个问题,提前致谢

回答by UI_Dev

There are several ways to achieve this.

有几种方法可以实现这一点。

Probably the easiest would be to use JavaScript to change the form's action.

可能最简单的方法是使用 JavaScript 来更改表单的操作。

<input type="submit" value="SecondServlet" onclick="form.action='SecondServlet';">

But this of course won't work when the enduser has JS disabled (mobile browsers, screenreaders, etc).

但是当最终用户禁用 JS(移动浏览器、屏幕阅读器等)时,这当然不起作用。

Another way is to put the second button in a different form, which may or may not be what you need, depending on the concrete functional requirement, which is not clear from the question at all.

另一种方法是将第二个按钮以不同的形式放置,这可能是您需要的,也可能不是您需要的,这取决于具体的功能需求,从问题中根本不清楚。

<form action="FirstServlet" method="Post">
    Last Name: <input type="text" name="lastName" size="20">
    <br><br>
    <input type="submit" value="FirstServlet">
</form>
<form action="SecondServlet" method="Post">
    <input type="submit"value="SecondServlet">
</form>

Note that a form would on submit only send the input data contained in the very same form, not in the other form.

请注意,表单在提交时只会发送包含在同一表单中的输入数据,而不是其他表单中。

Again another way is to just create another single entry point servlet which delegates further to the right servlets (or preferably, the right business actions) depending on the button pressed (which is by itself available as a request parameter by its name):

另一种方法是创建另一个单一入口点 servlet,它根据按下的按钮进一步委托给正确的 servlet(或者最好是正确的业务操作)(按钮本身可作为请求参数通过其名称获得):

<form action="MainServlet" method="Post">
    Last Name: <input type="text" name="lastName" size="20">
    <br><br>
    <input type="submit" name="action" value="FirstServlet">
    <input type="submit" name="action" value="SecondServlet">
</form>

with the following in MainServlet

与以下 MainServlet

String action = request.getParameter("action");

if ("FirstServlet".equals(action)) {
    // Invoke FirstServlet's job here.
} else if ("SecondServlet".equals(action)) {
    // Invoke SecondServlet's job here.
}