-1

I have jsp page with many buttons. When I pressed some button it handle in servlet, but how I can identify that button was pressed? I do that:

private String getPressedBtn(HttpServletRequest request){
    Enumeration<String> parameterNames = request.getParameterNames();
    String pressBtn = "";
    if(parameterNames.hasMoreElements()){
        pressBtn = parameterNames.nextElement();
    }
    return pressBtn;
}

But I think that it's very bad way. Wich way is properly?

dzrkot
  • 543
  • 2
  • 4
  • 23

2 Answers2

0

If you want to get pressed button in servlet, there is better way to do it: At first, use input tag for buttons, define common name attribute for buttons and specific value for each button.

Example:

<input type="button" name="button" value="button1" />
<input type="button" name="button" value="button2" />
...

Then in servlet you can get it by this way:

private String getPressedBtn(HttpServletRequest request){
    return request.getParameter("button");
}
eg04lt3r
  • 2,467
  • 14
  • 19
0

In each button, you put a link inside, all link have the same request address, but differrent button name in parameters.

Mai Hữu Lợi
  • 559
  • 2
  • 8
  • 18