Retrieving values of checkboxes created dynamically

Will someone help me in the code Retrieving values of checkboxes created dynamically using JSP. The checkboxes are check1,check2 ....... checkn. n is passed as a hidden parameter . Its urgent
[198 byte] By [ReshmaCBa] at [2007-11-27 5:06:17]
# 1

It doesn't matter if they are generated dynamically or not. As long as they are nested in the right <form> element, you can retrieve the selected values by HttpServletRequest#getParameterValues(). Also see [url=http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequest.html#getParameterValues(java.lang.String)]ServletRequest API[/url].

BalusCa at 2007-7-12 10:24:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thanks.I wnanted the code since it is urgent. Any help available.
ReshmaCBa at 2007-7-12 10:24:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

You're the developer here. I've pointed you to getParameterValues(). All you need to do is to implement it in your code. The API states that it returns a String[]. That's all enough to know, isn't it?

Or if you prefer putting pressure to us by shouting Urgent! I want code! Urgent! Give me code!, then you're a fake developer.

BalusCa at 2007-7-12 10:24:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

well i don't think request.getParameterValues(). is used here.

According to OP shes says she is dynamically populating set of check boxes with names check1,check2 & check3.....

to my understading we can use request.getParameterValues() when multiple values are associated to same name(control) for example a Multiple select Box where you select multiple values.

@OP

Well if you need get dynamically generated names and their associated values extracting from request. you might have to construct a Map with key parametersName and use it as per you need....

just in a given case i'm assuming that you need to have the hidden values of all the checkboxes.which starts with the name "check" and associated number(assuming it to be a single digit number) with it...

here is how you can do it...

public doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{

HashMap cbmap = new HashMap();

for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {

String param = (String) e.nextElement();

// checking whether the parameter names is of the pattern "check[0 -9]"

if(param.matches("check\d")){

String value = request.getParameter(param);

cbmap.put(param,value);

}

}

-

--

--

}

Please excuse me if i was wrong with my understading(Users description).

Hope this might help :)

However,i'd still stand on what my fellow poster have said ppl here will not provide you the code they can give you some advices its your responsiblity to implement things out on the thoughts provided.

REGARDS,

RaHuL

RahulSharnaa at 2007-7-12 10:24:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> According to OP shes says she is dynamically

> populating set of check boxes with names

> check1,check2 & check3.....

Exactly. And he (she?) can retrieve the values using request.getParameterValues("checkn"). Not using request.getParameter("checkn"), it will return only the latest checked value of the group otherwise.

BalusCa at 2007-7-12 10:24:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

@BalusC

Sorry i was just reconfirming on it... :)

Hope there are no hard issues :)

Hope you have a gr8 day

@OP

Guess the below code snippet wud be more brief.

public doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{

HashMap cbmap = new HashMap();

int n = Integer.parseInt(request.getParameter("n"));

for(int i = 1 ; i <= n ; i++){

String str = request.getParameter("check"+i);

// String str[] = request.getParameterValues("check"+i);

cbmap.add("check"+i,str);

}

-

--

--

}

REGARDS,

RaHuL

RahulSharnaa at 2007-7-12 10:24:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

The Script

function fnAdd(){

var Cnt=eval(thisForm.hidcnt.value) + 1;

if(Cnt<5){

var newtr=Add.insertRow();

var newtd1=newtr.insertCell(); newtd1.bgColor="#FFFFFF";

newtd1.innerHTML +="<input type=text name=notes[]"+Cnt+" maxlength='255' class='txtbox' style=width:180;>";

document.thisForm.hidcnt.value=Cnt;

}else{

alert("Sorry you are not allowed to add more than 5 notes");

}

}

The JSP Code

<tr bgcolor="white"><td valign='top'><b>Notes</b></td><td>

<table border='0' id='Add'>

<tr bgcolor="white"><td>

<input type="TEXT" name="notes[]" class="txtbox" maxlength="255"> <a onClick='fnAdd();' style='cursor:hand'>Add New Note</a></td>

</tr>

</table>

</td></tr>

The Servlet Code:

String notes0= request.getParameter("notes[]");

String notes1= request.getParameter("notes[]1");

String notes2= request.getParameter("notes[]2");

String notes3= request.getParameter("notes[]3");

String notes4= request.getParameter("notes[]4");

String notes= notes0+"~"+notes1+"~"+notes2+"~"+notes3+"~"+notes4+"~";

art84a at 2007-7-12 10:24:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Thanks , the snippet helped me solve the problem, i shall be careful when requesting hence forth.
ReshmaCBa at 2007-7-12 10:24:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
Thanks for the tip. I shall be more careful next time.
ReshmaCBa at 2007-7-12 10:24:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...