Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 19th, 2003, 03:14 PM
OConnor OConnor is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 9 OConnor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Working with multiple records (JSP)

Hello, I'm new here. *grins hopefully*

I'm building a web site for work (first time) that uses JSP calling Java classes to provide database access, and there's just one problem I can't work out.

If a search returns multiple records, how do I select only ne to be updated/deleted? Currently, if I put my jsp:setProperty tag above the For loop that prints the records, I can only peg the first record returned. If I put it inside the loop, nothing. I know the problem is the value of my "currentRecord" bean is not being changed, but how do I fix that? (The udpate/delete does work on the first record, along with everyting else... Finally).

Any suggestions?

(If anyone wants to know how I got this far, I'd be happy to help).

OConnor

Reply With Quote
  #2  
Old February 19th, 2003, 05:50 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
We need more information. Can you post all the code that is relevant to the page you are talking about?

Reply With Quote
  #3  
Old February 25th, 2003, 01:38 PM
OConnor OConnor is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 9 OConnor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Here it is without the exessive HTML. The method names are should be self-explanatory.

<head>

***Usual stuff***

<jsp:useBean class="nfmc.ContactList" id="contactsList" scope="page" />
<jsp:useBean class="nfmc.Contact" id="currentContact" scope="page" />
<jsp:useBean class="nfmc.Contact" id="searchedContacts" scope="session" />

</head>
<body>

<form method="POST" action="admContacts.jsp">
<%-- If doing update, don't zonc values --%>
<% if (request.getParameter("B4") == null) { %>
<% searchedContacts.clearContact(); %>
<jsp:setProperty name="searchedContacts" property="type" />
<jsp:setProperty name="searchedContacts" property="contact" />
<jsp:setProperty name="searchedContacts" property="company" />
<% } %>

**The form code is long because it involves loading database values into list boxes.
Needless to say it has widgets named type, contact & contact name.***
</form>

<% if (request.getParameter("B1") != null) { %>
<%-- Do search --%>
<% contactsList.search(searchedContacts); %>

<%-- Tell user how many records were found --%>
<p align="center"><font face="Arial" size="3"><%= contactsList.sendRecordsFound() %>
</font></p>
<% } %>


<table border="0" width="100%">
<form method="POST" action="admContacts.jsp">
<jsp:setProperty name="currentContact" property="*" />
<% for (int i = 0; i < contactsList.getTotalRecord(); i++) { %>

<% if (contactsList.getTotalRecord() > 0) { %>
<% currentContact = contactsList.getCurrent(); %>
<% } %>
<tr>
<td width="20%"><font face="Arial" size="3">Contact's Name:</font></td>
<td width="20%"><input type="text" name="contact" value="<%=currentContact.getContact() %>"></td>
<td width="20%"><font face="Arial" size="3">Title:</font></td>
<td width="20%"><input type="text" name="division" value="<%=currentContact.getDivision() %>"></td>
</tr>

***etc,etc***

</tr>
<% contactsList.nextRecord(); %>

<td width="100%" colspan="4"><%-- just a record seperator--%> -----------------------------------------------
--------------------------------------------------------------------------------</font></td>
</tr>
<% } %> <%-- End for loop --%>
</table>
<%-- Only show update & delete buttons if a search has been done --%>
<% if (contactsList.getTotalRecord() > 0) { %>
<input type="submit" name="B4" value="Update Record"><input type="submit" value="Delete Record" name="B5">
<% } %>
</font>

<%-- Update button code --%>
<% if (request.getParameter("B4") != null) { %>
<font face="Arial"><%= currentContact.update() %></font><br>
<font face="Arial"><%= currentContact.sendError() %></font>
<% } %>
</form>

***Basically same thing for the delete button***
</body>

Reply With Quote
  #4  
Old February 25th, 2003, 04:43 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
OK, this appears to be a problem with using jsp tags and scriptlets (<% %>) tags together. Jsp tags are supposed to "save" us from dreaded programming code in our jsp's, but sometimes it just confuses us to whats going on.

I made a sample page and looked at the code that was generated by the web server. What appears to be happening is kind of a scope issue.

When you use a jsp:useBean tag, the server creates a bean of the specified type as a local variable in the service method. It also puts a copy of that in the context that you specify. in this case it puts a copy of the bean in it's pageContext. So in essence we have 2 references to the same bean.

Now, when you call this line of code:
Code:
<jsp:setProperty name="currentContact" property="*" />

What it is doing is retrieving the bean from the pageContext and setting that objects values to the values passed in the request. Fine.

However, when you call this line of code:
Code:
<% currentContact = contactsList.getCurrent(); %>

you are changing the local method variable reference to something other than what is stored in the pageContext. So, if you put the setProperty property="*" in the for loop, all it is changing is the values of the bean stored in the pageContext, not the local variable you are dereferencing in your loop. Whew.

I hope you followed that, cause it can get confusing. Personally, what I would do is remove the jsp:useBean tag and instantiate the bean yourself the 'normal' way. I.E.,
Code:
Instead of:
******
<jsp:useBean class="nfmc.ContactList" id="contactsList" scope="page" />
<jsp:useBean class="nfmc.Contact" id="currentContact" scope="page" />
<jsp:useBean class="nfmc.Contact" id="searchedContacts" scope="session" />

Use something like this:
******
<%
nfmc.ContactList contactList = new nfmc.ContactList();
nfmc.Contact currentContact = new nfmc.Contact();
nfmc.Contact searchedContacts = (nfmc.Contact)session.getAttribute("searchedContacts");
if(searchedContacts == null) {
  searchedContacts = new nfmc.Contact();
}
%>


Try removing all the jsp:setProperty tags and setting the properties yourself and see if this doesnt start working.

I have attached the jsp source as created by the web server for anyone interested.
Attached Files
File Type: java _testbean.java (5.3 KB, 282 views)

Reply With Quote
  #5  
Old February 26th, 2003, 03:35 PM
OConnor OConnor is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 9 OConnor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you very much. I shall foward this on to the programmer here at work who told me to do it this way, haha.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Working with multiple records (JSP)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT