#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    18
    Rep Power
    0

    Strange problem in retrieveing data from a table -Please help me


    Hi all ,
    I am developing a bookstore application for my school project with JSP ,JAVA Beans and servlets.

    Initially user goes to the home page and searches for a book using some criteria and it pulls the matching records.
    For example

    Author isbn price

    JavaProgramming 157668 35 AddTo Cart
    JavaServlets 548698 45 AddTo Cart


    For the above output the following is my html code

    <form action="/IS_Project/jsp/addtocart.jsp">
    <table border=1 align=center >
    <tr>
    <td>Author</td><td>Title</td><td>Price</td><td>ISBN</td></tr>
    <% while ( it.hasNext() )
    {
    book = (Book)it.next(); %>
    <tr name="item">
    <td><% out.println(book.getAuthorFirstName()+book.getAuthorLastName()); %></td>
    <td><% out.println(book.getTitle()); %></td>
    <td><% out.println(book.getPrice()); %></td>
    <td><% out.println(book.getIsbn()); %></td>
    <td><input type=submit value="ADDTO CART"></td>
    </tr>
    <%
    }
    }
    catch(Exception e)
    {
    System.out.println(e.getMessage());
    }
    %>
    </table>
    </form>


    Here when the user clicks on Addto Cart i am calling the addtocart.jsp.In the addtocart.jsp i am callinh a java bean.

    Now i want to pass the isbn of the book user selected ,
    mu question is whether the table row supports name parameter , if it does how to retrieve the isbn value.

    Here i don't want to use a drop down box , i want to show the results as i shown above like in row a column fashion.What is best form element which displays as i shown and at the same time i can retrieve the values using the request object.


    Here is my jsp , if it helps

    <%@ page import="com.archana.is_project.bookstore.user.beans.UserAccount" %>
    <%@ page import="java.sql.*" %>

    <html>
    <body bgcolor="#FFDAB9">
    <%! UserAccount userinfo; %>

    <%
    userinfo = (UserAccount) session.getAttribute("userinfo");
    if (userinfo == null)
    {
    out.println("<h2 align=center>you are not loged in, please login below</h2>"); %>
    <jsp:include page="login.html" />

    <% }
    else {
    //here i want to call the bean , including the user selcted book information
    out.println("your book is added to your cart");
    }
    %>
    </body>
    </html>

    Please help me, as i have to submit my school project in 15 days , I am tensed up.

    Archana
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Wow, where to start....

    First off, I am having a hard time understanding what you are asking. My guess is that english is your second language? For instance:
    Now i want to pass the isbn of the book user selected ,
    mu question is whether the table row supports name parameter , if it does how to retrieve the isbn value.

    Here i don't want to use a drop down box , i want to show the results as i shown above like in row a column fashion.What is best form element which displays as i shown and at the same time i can retrieve the values using the request object.
    I have read these sentences several times and still can't quite get what you are saying. Try rephrasing the question please.

    Lastly, I notice in your bottom jsp that you are instantiating the UserAccount userinfo bean as a instance variable of the jsp(servlet). This is in all likelyhood a mistake. If you do this, then everyone accessing the jsp will be using the same variable. If someone requests this jsp and then someone requests it milliseconds later, then it is possible that user one will be using the values from user two.

    Instead of using
    Code:
    <%! UserAccount userinfo; %>
    
    
    Use this instead:
    
    <% UserAccount userinfo; %>
    The bang (!) symbol is the difference. By using the declaration tag you are making this a 'global' variable instead of 'local' variable.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    18
    Rep Power
    0
    It seems I confused you with so much unnecessary description.

    The question I have is

    for example for text area we say
    <input type= text name="item" size=25>

    and to retrieve the value of that text area in a Java bean we say

    request.getParameter(item);

    If you look at my html code now I am displaying the books description in the table format and there is a AddToCart button beside each book description.When the user clciks on the AddToCart I am calling the jsp,now in the JSP I would like to retrieve the value of the book user added to their cart.

    Could you tell me how i can do that

    Archana
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    OK, I think I see now. The problem as I see it is that you have multiple books. Somehow you are going to have to utilize some hidden fields. Something like this:
    Code:
    <% while ( it.hasNext() ) 
    { 
    book = (Book)it.next(); %>
    <tr name="item">
    <td><%= book.getAuthorFirstName()+book.getAuthorLastName() %><input type=hidden name=author value="<%= book.getIsbn() %>"></td> 
    <td><%= book.getTitle() %><input type=hidden name=title value="<%= book.getIsbn() %>"></td> 
    <td><%= book.getPrice() %><input type=hidden name=price value="<%= book.getIsbn() %>"></td>
    <td><%= book.getIsbn() %><input type=hidden name=isbn value="<%= book.getIsbn() %>"></td>
    <td><input type=submit value="ADDTO CART"></td>
    </tr>
    <% 
    }
    Now we have our data in form fields that we can submit. A problem now is that all the books will have the same name for their fields and when you do a getParameter("isbn") you will get the first books and not the one the user was actually submitting for(hope that made sense).

    To get around this I would normally wrap each book in its own form. Each form would submit to the same page, but only the hidden fields for that form would be submitted. The problem is that you cannot put a form tag in between rows of a table, so that won't work. If each book was in it's own table we could do this. Something like this maybe:
    Code:
    <% while ( it.hasNext() ) 
    { 
    book = (Book)it.next(); %>
    <form action="/IS_Project/jsp/addtocart.jsp">
    <table border=1 align=center >
    <tr>
    <td>Author</td><td>Title</td><td>Price</td><td>ISBN</td></tr>
    <tr name="item">
    <td><%= book.getAuthorFirstName()+book.getAuthorLastName() %><input type=hidden name=author value="<%= book.getIsbn() %>"></td> 
    <td><%= book.getTitle() %><input type=hidden name=title value="<%= book.getIsbn() %>"></td> 
    <td><%= book.getPrice() %><input type=hidden name=price value="<%= book.getIsbn() %>"></td>
    <td><%= book.getIsbn() %><input type=hidden name=isbn value="<%= book.getIsbn() %>"></td>
    <td><input type=submit value="ADDTO CART"></td>
    </tr>
    </table>
    </form>
    <% 
    }
    The you will only get the fields for that particular book. This may cause unwanted spaces between the tables. You can eliminate then using an inline style on the form tag
    Code:
    <form action="/IS_Project/jsp/addtocart.jsp" style="display:inline">
    You will have to see what this does. It may cause unwanted behavior with your tables.

    Other than this you will have to use some sort of creative programming. You could use some javascript client side. You could have one set of hidden book fields that are global and when the user clicks submit then you could populate those hidden fields with the appropriate values.

    Or maybe you could have a hidden field called "index". You would set this index when the user clicked on the submit butten. When you get the values in your servlet/jsp, you would use String[] isbns = getParameterValues("isbn"). This returns all the isbn numbers that were submitted as a String[] array. You would get the index using getParameter("index"). Turn the index into an int using Integer.parseInt(String). Then you could get the one the user wanted by using isbns[index].

    The javascript would look something like:
    Code:
    <script>
    	function setIndex(index) {
    		var field = document.getElementById("index");
    		field.value = index;
    	}
    </script>
    <form action="/IS_Project/jsp/addtocart.jsp">
    <input type="hidden" name="index" id="index">
    <table border=1 align=center >
    <tr>
    <td>Author</td><td>Title</td><td>Price</td><td>ISBN</td></tr>
    <% int count = 0;
    	while ( it.hasNext() ) 
    	{ 
    	book = (Book)it.next(); %>
    <tr name="item">
    <td><%= book.getAuthorFirstName()+book.getAuthorLastName() %>
    	<input type=hidden name=author value="<%= book.getIsbn() %>"></td> 
    <td><%= book.getTitle() %><input type=hidden name=title value="<%= book.getIsbn() %>"></td> 
    <td><%= book.getPrice() %><input type=hidden name=price value="<%= book.getIsbn() %>"></td>
    <td><%= book.getIsbn() %><input type=hidden name=isbn value="<%= book.getIsbn() %>"></td>
    <td><input type=submit value="ADDTO CART" onclick="setIndex(<%= count %>)"></td>
    </tr>
    <% count++;
    	} 
    }
    catch(Exception e)
    {
    System.out.println(e.getMessage());
    }
    %>
    </table>
    </form>
    Hope that all made sense.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    18
    Rep Power
    0
    Nemi , That is really great help .Thank you very much.I will try all the ways you told.I will see which one works for me.

    Archana

IMN logo majestic logo threadwatch logo seochat tools logo