aspose file tools*
The moose likes JSP and the fly likes sending  updated array of javaBeans from JSP back to Servlet! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "sending  updated array of javaBeans from JSP back to Servlet!" Watch "sending  updated array of javaBeans from JSP back to Servlet!" New topic
Author

sending updated array of javaBeans from JSP back to Servlet!

Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
In my sevlets I make a databse call and am storing each row of the resultset in my JavaBean class. ie
myBean[0] - 1st row
mybean[1] - 2nd row
:
:
so on

I am sending the array of beans to JSP. And in JSP I display the data in a table

<jsp:useBean id="bean" class="com.property.MyBean">
<%
//I am getting the array of beans somehow here and lets think that myBean is that array %>
//Display the data here
:
for(int i=0; i<myBean.length; i++)
{
<tr>
<td><input type="text" id="length" value= <%=mybean[i].getLength()%> </td>
<td><input type="text" id="width" value= <%=mybean[i].getWidth()%> </td>
<td><input type="text" id="length" value= <%=mybean[i].getDescription()%> </td>
</tr>
}
So the JSP display a table with 3 colums and n rows.
The user is allowd to make changes to the data and when he hits ok, I have to get the updated data from the JSP, back to the servlet.
How do I do that...
Please give a suggestion.
Thanks
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
I forgot to mention, the user is allowed to enter a new rows too....
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
When the user changes the date or not, you can have the click of the button post the data to the servlet, and let teh servlet do the update to the DB.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
Yes, but how do I loop through the data in the Servlet...
ie say in my JSP output is:
______________________________
Length width Description
______________________________
2 3 description1
2 4 description2
3 5 description3
6 4 description4
Lets say the user changes some of the vaues in the above table, how should I access each of the above row in the Servlet.
I should be able to put the values of the first row in myBean[0], 2nd row in myBean[1]...so on.
Ho do I do that? Thanks...
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
I know how to update the bean using setProperty. But that updates just one javaBean object.
<jsp:useBean id="SearchForm" class="SearchForm" scope="request">
<jsp:setProperty name="SearchForm" property="*"/>
</jsp:useBean>
But if I have a array of javaBeans, can I use the same way to update the array..
<jsp:useBean id="searchForm" class="SearchForm" scope="request">
<jsp:setProperty name="SearchForm" property="*"/>
</jsp:useBean>
<% SubSearchForm[] myBean = searchForm.getSubSearchForms(); %>
When the user updates the myBean array, will the above setProperty takes care updating the myBean array?
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
The previous poster was suggesting that you NOT update the beans in the JSP! (With which I heartily agree).
The beans are in the request parameter. They are GONE when that JSP executes and returns the HTML to the user. When you submit the changes there beans ARE NOT THERE.
Instead, you need to parse the paramters from the user in the servlet you post to (using getParameter()) and then use that information to update the database.
Also, your JSP is simply using the same id for every row in the table. Instead, you need to use a different id for each individual column. So append the index to the name to form the full id of each input field (something like ("length" + i), etc. That will result in parameters named length0, length1, length2, width0, width1, width2, ... being returned to the servlet, which you can use to determine how to update the database.
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sending updated array of javaBeans from JSP back to Servlet!