调用不同Servlet的同一表单中的多个提交按钮 [英] Multiple submit buttons in the same form calling different Servlets

查看:172
本文介绍了调用不同Servlet的同一表单中的多个提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是代码:

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

我想了解如何在 FirstServlet按钮的情况下发送信息被按下 FirstServlet ,如果 SecondServlet按钮被按下 SecondServlet

I'd like to understand how to send information in case the FirstServlet button was pressed to FirstServlet and in case the SecondServlet button was pressed to SecondServlet.

重要:

我想在相同的形式,因此相同的信息将被转换到两个servlet。 (当然,在servlet中我将相应地使用信息)

important:
I want to do it in the same form so the same information would be transered to both the servlets. (of course, in the servlets I'll use the info accordingly)

推荐答案

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

There are several ways to achieve this.

可能最简单的方法是使用JavaScript来改变表格的动作。

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

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

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

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

另一种方法是将第二个按钮放在不同的形式,这可能是也可能不是你需要的根据具体的功能要求,这个问题根本不清楚。

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.

另一种方法是再创建另一个单一入口点servlet,它进一步委托给正确的servlet(或者最好是正确的商业行为)取决于按下的按钮(它本身可以通过名称作为请求参数提供):

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):

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

以下 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.
}

这只是i18n /维护友好。如果您需要在忘记考虑servlet代码的情况下显示不同语言的按钮或更改按钮值,该怎么办?

This is only not very i18n/maintenance friendly. What if you need to show buttons in a different language or change the button values while forgetting to take the servlet code into account?

稍微改变是给按钮自己固定且唯一的名称,以便可以检查它作为请求参数的存在而不是它对i18n /维护敏感的值:

A slight change is to give the buttons its own fixed and unique name, so that its presence as request parameter could be checked instead of its value which would be sensitive to i18n/maintenance:

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

以下 MainServlet

if (request.getParameter("first") != null) {
    // Invoke FirstServlet's job here.
} else if (request.getParameter("second") != null) {
    // Invoke SecondServlet's job here.
}






最后一种方式是只需使用像 JSF 这样的MVC框架,这样你就可以直接将javabean方法绑定到按钮,但这需要对你的现有代码。


Last way would be to just use a MVC framework like JSF so that you can directly bind javabean methods to buttons, but that would require drastic changes to your existing code.

<h:form>
    Last Name: <h:inputText value="#{bean.lastName}" size="20" />
    <br/><br/>
    <h:commandButton value="First" action="#{bean.first}" />
    <h:commandButton value="Second" action="#{bean.Second}" />
</h:form>

只有以下javabean而不是servlet

with just the following javabean instead of a servlet

@ManagedBean
@RequestScoped
public class Bean {

    private String lastName; // +getter+setter

    public void first() {
        // Invoke original FirstServlet's job here.
    }

    public void second() {
        // Invoke original SecondServlet's job here.
    }

}

这篇关于调用不同Servlet的同一表单中的多个提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆