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

查看:87
本文介绍了同一表单中的多个提交按钮调用不同的 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/maintenance 很敏感:

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天全站免登陆