how to get the button value from jsp to servlet

2019-01-15 02:00发布

how to get the button value from jsp to servlet in jsp:

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

and in servlet like that:

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

result=null

thanks

标签: jsp servlets
3条回答
放我归山
2楼-- · 2019-01-15 02:35

Take a hidden variable inside form and use it like this.

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

Now, in the servlet,

String gi =request.getParameter("hdnbt");
System.out.print("button value" +gi);
查看更多
Anthone
3楼-- · 2019-01-15 02:56

Rather use <input type="submit">.

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

Its name/value pair will be sent to the server side as well:

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

No need for JavaScript hacks/workarounds here. It would also break your application in case that the client has JavaScript disabled.

查看更多
Summer. ? 凉城
4楼-- · 2019-01-15 02:57

You need to convert the button parameter to String using .toString(). There is nothing wrong with your code.

查看更多
登录 后发表回答