1

I've got such form on my page

<form action="ToolbarAction.do" method="POST">
    <div id="toolbar"
        class="ui-widget-header ui-corner-all ui-widget-content">
        <input style="font-family: times new roman;" type="button" id="item0"
            value="<fmt:message key='main'/>" /> <input
            style="font-family: times new roman;" type="button" id="item1"
            value="<fmt:message key='prices'/>" />
        <c:choose>
            <c:when test="${enterAttr == false || enterAttr == null }">
                <input style="font-family: times new roman;" type="submit"
                    id="item2" value="<fmt:message key='book'/>" />
            </c:when>
            <c:when test="${enterAttr == true }">
                <input style="font-family: times new roman;" type="submit"
                    id="item2" value="<fmt:message key='check'/>" />
            </c:when>
        </c:choose>
        <input style="font-family: times new roman;" type="submit" id="item3"
            value="<fmt:message key='contacts'/>" /> <input
            style="font-family: times new roman;" type="submit" id="item4"
            value="<fmt:message key='service'/>" />
    </div>
</form>

How to check what button was pressed and caused the ToolbarAction? Here exec method in ToolbarAction classs. I should get some parameters from HttpServletRequest?

public String exec(HttpServletRequest req, HttpServletResponse resp) {
    // TODO Auto-generated method stub
    return null;
}
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
lapots
  • 12,553
  • 32
  • 121
  • 242
  • 3
    Give your buttons names. They will then appear as request parameters which you can retrieve from the `HttpServletRequest`. – Sotirios Delimanolis Jul 25 '13 at 14:44
  • Oh! So it sends in request only name of button which caused the action? – lapots Jul 25 '13 at 14:46
  • 1
    A form can only be submit with one `submit` input. It will send the one that the user clicked as a request parameter if it has a `name` attribute. – Sotirios Delimanolis Jul 25 '13 at 14:49
  • So...What button I should use as `submit`? Or I can remain it like this? Also - every button should have the same name? – lapots Jul 25 '13 at 14:55
  • possible duplicate of [How do I call a Java method on button click event of JSP or HTML?](http://stackoverflow.com/questions/14723812/how-do-i-call-a-java-method-on-button-click-event-of-jsp-or-html) – BalusC Jul 25 '13 at 15:27

1 Answers1

1

The solution is to give the same name attribute to all your <input> elements.

<input name="submit" style="font-family: times new roman;" type="submit"
                id="item2" value="<fmt:message key='check'/>" />

Since only one submit button can be pressed by the user for each request, you will have a single request parameter called submit. You can retrieve it like

String value = request.getParameter("submit");

where request is the HttpServletRequest object. The return value of getParameter is the value attribute of the <input> element. You can do a bunch of if checks to see which was pressed.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • But what if value changes depends on language?`value=""` - different button values. – lapots Jul 25 '13 at 15:07
  • You should use a constant value of the `value` attribute, but change the text element based on internalization: `` – Sotirios Delimanolis Jul 25 '13 at 15:11
  • Actual value is irrelevant. Just give them all an unique name and check if it is not null as param. – BalusC Jul 25 '13 at 15:26
  • Er...But value should have something - button name for example can be either `main` or `главная`. – lapots Jul 25 '13 at 15:42
  • 1
    @user1432980 but the real value does not matter. The important tag parameter is _name_. eg __ and __ get both be retrieved by _String button1 = request.getParameter("submit1")_ and _String button2 = request.getParameter("submit2")_. Then you compare _button1_ and _button2_ and the one **NOT NULL** did the submit – fGo Jul 26 '13 at 12:57