如何从jsp获取按钮值到servlet

时间:2010-05-26 11:18:58

标签: jsp servlets

如何从jsp获取按钮值到servlet 在jsp:

<input type=button name=bt value=gi onclick="document.frm.submit();"></input>

和servlet一样:

String gi =request.getParameter("bt");
    System.out.print("button value" +gi);

result = null

谢谢

3 个答案:

答案 0 :(得分:26)

而是使用<input type="submit">

<input type="submit" name="bt" value="gi">

它的名称/值对也将被发送到服务器端:

String bt = request.getParameter("bt"); // gi

这里不需要JavaScript hacks / workarounds。如果客户端禁用了JavaScript,它也会破坏您的应用程序。

答案 1 :(得分:3)

在表单中取一个隐藏变量并像这样使用它。

<form name="frm" method="post" action="">
<input type="hidden" name="hdnbt" />
<input type="button" name="bt" value="gi" onclick="{document.frm.hdnbt.value=this.value;document.frm.submit();}" />
</form>

现在,在servlet中,

String gi =request.getParameter("hdnbt");
System.out.print("button value" +gi);

答案 2 :(得分:-3)

您需要使用.toString()将button参数转换为String。您的代码没有任何问题。

相关问题