Hi,

a) I need to display list of ItemName, ItemLocation in a table row with a check box in the left-most column.

b) User can select multiple jobs and on clicking "Submit" button I should pass the selected ItemName and ItemLocation to a fuction in java.

I am able to get the list of items using Item bean java class in Arraylist but need suggestion on what will be the best approach so that I can pass each selected ItemName and ItemLocation to a fuction in java.

Please suggest, any sample would be of great help.

Regards

I expect that each of your items has unique value that should be assigned as value to each check box when you populate table. Then on submit you will get set of check box values that represent unique item IDs and these will enable you to recover relevant data from DB

I expect that each of your items has unique value that should be assigned as value to each check box when you populate table. Then on submit you will get set of check box values that represent unique item IDs and these will enable you to recover relevant data from DB

Thanks for the response...

Yes, I can consider ITEM_ID as unique and should pass corresponding values (ITEMNAME, ITEMLOCATION, STATUS) to a java function which will call procedure.

Example :

Database table (ITEM_RENEW) for Items :
ITEM_ID ITEMNAME ITEMLOCATION STATUS LST_DATE
25 CCC //L123/R FAILED 07-JAN-10 04.48.54 AM
23 AAA //L123/R RENEWED 07-JAN-10 04.48.54 AM
24 BBB //L123/R FAILED 07-JAN-10 04.48.54 AM

I have ItemDO java class where I am executing simple query to get the data...
SELECT ITEM_ID, ITEMNAME, ITEMLOCATION, STATUS, LST_DATE FROM ITEM_RENEW ORDER BY LST_DATE DESC.

Should I use simple ArrayList or HashMap as I need to pass selected checkbox values...

> Should I use simple ArrayList or HashMap as I need to pass selected
> checkbox values...

Just ensure that all your checkbox elements have the same name and then use the HttpServletRequest.getParamterValues('elementName') to retrieve a String array containing the list of item id's.

OK, here is what I am able to achieve using jstl in jsp page...

Please help on the below issue :

a) I am setting the values from database to itemBean
b) In the below servlet I am setting the items in session

c) In JSP page (please see code below, I have also attached screen shot) I am able to display the records in table

Now, issue is if I check few checkboxes and on clicking submit how to pass each row data values to a function in java, pls suggest ... stuck ..(

ItemDO.java:------------

public ArrayList<ItemBean> getItems(){
	ArrayList<ItemBean> itemList = new ArrayList<ItemBean>();
	...
		try{	    		
			String query = " SELECT ITEM_ID, ITEMNAME, ITEMLOCATION, STATUS, LST_DATE " +
			"FROM ITEM_RENEW  " +
			"ORDER BY LST_DATE DESC";
			
			statement = conn.createStatement();
			rs = statement.executeQuery(query);
			while(rs.next()){
				ItemBean itemBean = new ItemBean();
				itemBean.setItemId(rs.getLong("ITEM_ID"));
				itemBean.setItemName(rs.getString("ITEMNAME"));	    			
				itemBean.setItemLocation(rs.getString("ITEMLOCATION"));
				itemBean.setStatus(rs.getString("STATUS"));
				itemBean.setDateCreated(rs.getString("LST_DATE"));
				itemList.add(itemBean);
			}	    	
		}
	....		
	return itemList;
}

Servlet:-------

private ArrayList<ItemBean> items;
...
ItemDO dm = new ItemDO();	
try {
	session.setAttribute( "userBean", userBean);
	items = new ArrayList<ItemBean>(dm.getItems());
	session.setAttribute("items", items);
	RequestDispatcher dispatcher = request.getRequestDispatcher("/updateitem.jsp");
	dispatcher.forward( request, response);
}

updateitem.jsp:
--------------

<html>
<body>
<form name="form1" method="post" action="login?requestPage=updateItm">
<table>
	<tr>
    	<th>Below are the list of items with status</th>
    </tr>
	<tr>
		<td>Select</td>    	
        <td>Item Id</td>
        <td>Item Name</td>
        <td>Item Location</td>
        <td>Status</td>
        <td>Last Updated</td>
    </tr>   
    <c:forEach var="item" items="${items}">
    	<tr>  
    	  	<td>
    	  		<input type="checkbox" name="itemId" value="${item.itemId}">
    	  	</td>	
    		<td>${item.itemId}</td>
    		<td>${item.itemName}</td>
    		<td>${item.itemLocation}</td>
    		<td>${item.status}</td>
    		<td>${item.dateCreated}</td>
    		</tr>		
    </c:forEach>     
</table>
<table>
	<tr>
		<td>
			<INPUT id="submit" name="submit" type="submit" value="Submit Items"/>
		</td>
	</tr>
</table>
</form>
</body>
</html>

> if I check few checkboxes and on clicking submit how to pass each
> row data values

Read the replies posted by me and Peter again...

Yes thanks...

I am able to get the ids in servlet which I can use further..
Though I can use these ids to query and get the relevant details....
In this case I need to hit database to query... is there any way around if I can pass all corresponding values (itemName, itemLocation, status) for SELECTED checkbox...

JSP :

<c:forEach var="item" items="${items}">
    	<tr>  
    	  	<td>
    	  		<input type="checkbox" name="idValue" value="${item.itemId}">    	  		
    	  	</td>	
    		<td>${item.itemName}</td>
    		<td>${item.itemLocation}</td>
    		<td>${item.status}</td>
    		<td>${item.dateCreated}</td>
    		</tr>		
    </c:forEach>

Servlet:

String[] idsToDelete = request.getParameterValues("idValue");
for (int i = 0; i < idsToDelete.length; i++) {		 
	System.out.println(idsToDelete[i] + "<br>");
}

I was thinking to pass values using some delimiter in JSP
<input type="checkbox" name="idValue" value="${item.itemId}|${item.itemName}|${item.itemLocation}">
Then split the values in java to use futher... but dosen't look perfect...

> is there any way around if I can pass all corresponding values
> (itemName, itemLocation, status) for SELECTED checkbox

That would be a flawed approach since the client can easily intercept the request and inject spurious data in it corresponding to your item ID. I've always followed the rule of thumb to *not* pass something which can be retrived by your business layer. The risk involved in sending the data from the client is far more than the performance implications of calling the query again. BTW, if you are implementing a kind of item processing system, why not just work with ITEM ID's instead of dealing with all the details again?

> is there any way around if I can pass all corresponding values
> (itemName, itemLocation, status) for SELECTED checkbox

That would be a flawed approach since the client can easily intercept the request and inject spurious data in it corresponding to your item ID. I've always followed the rule of thumb to *not* pass something which can be retrived by your business layer. The risk involved in sending the data from the client is far more than the performance implications of calling the query again. BTW, if you are implementing a kind of item processing system, why not just work with ITEM ID's instead of dealing with all the details again?

You are right, instead of messing with the values, I should simply query on the basis of ID's...

Though I can try using some hashMap or so... but it would make code more tricky...

Actually there was a flaw with the design and I am suppose to pass specific values (itemName, itemLocation, status) to a proceduer which we already have..

Thank you all so much ...

i want to search items between min & max value which is entered by user.how can it possible?please help me.

<s:iterator value="pageList" var="quesvar" status="questat"> <s:hidden value="%{pageList[#questat.index].pageID}" name="pageDTO.pageID" id="pageID"></s:hidden> <s:hidden value="%{pageList[#questat.index].roleID}" name="pageDTO.roleID" id="roleID"></s:hidden> <s:hidden value="%{pageList[#questat.index].rolePageMapID}" name="pageDTO.rolePageMapID" id="rolePageMapID"></s:hidden> <tr class="item"> <td valign="top" class="style11" style="width: 20%;"> <s:checkbox name="pageDTO.rolePageMapID" id="isSelect" value="" styleClass="isSelected" multiple="true" fieldValue="Y" theme="simple"/></td> <td valign="top" class="style11" style="width: 20%;"><s:property value="pageList[#questat.index].pageCode"/></td> <td valign="top" class="style11" style="width: 20%;"><s:property value="pageList[#questat.index].pageName"/> </td> <td valign="top" class="style11" style="width: 20%;"><s:checkbox name="pageDTO.allRights" value="%{pageList[#questat.index].allRights}" styleClass="isSelected" fieldValue="1" theme="simple"/></td> </tr> </s:iterator>

    I want to show selected checkbox some are coming from database
    I want to checked some extra value also how can I pass into ajax such values .
    as checkbox list values coming from database
Member Avatar for diafol

Start a new discussion. Don't necropost.
Place your markup in code block and indent accordingly - this is unreadable.
Expand on the question - it does not explain what this data is or what you want to do with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.