开发者

Setting java string variables value depending on html checkbox selected

I have two check boxes with table names when any check box is checked i wants to save that table name in to a java s开发者_如何学Ctring which i can use in the query to get data from that table or update that table. I have used onClick functions and also got the check box value but not getting how to access it in the rest of the code so that i can use that value in the DB query.


I assume you have HTML code like the following:

<input type="checkbox" name="use_table1" />
<input type="checkbox" name="use_table2" />

On the server-side Java code, you can then query:

String tableName = null;
if (request.getParameter("use_table1") != null)
  tableName = "tbl_1";
if (request.getParameter("use_table2") != null)
  tableName = "tbl_2";

Note that the "outside" names differ from the real table names. No one out there on the web should need to know your real table names. And, most important, no one should be allowed to read an arbitrary table from your database. That's why I used that if-then-else code to select the table name.


Just give the table name as value. The browser will send only the name-value pairs of the checked ones as request parameters.

<input type="checkbox" name="tablename" value="table1">
<input type="checkbox" name="tablename" value="table2">
<input type="checkbox" name="tablename" value="table3">
<input type="checkbox" name="tablename" value="table4">

In the Servlet, you can grab them by HttpServletRequest#getParameterValues().

String[] checked = request.getParameterValues("tablename");

Simple as that :) No need for unnecessary JavaScript hacks/workarounds which ain't going to work in JS-disabled clients. This is one of the many unknown features of HTML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜