| Author |
Strange problem in retrieving the table data in a jsp
|
Archana Annamaneni
Ranch Hand
Joined: Jan 29, 2003
Posts: 147
|
|
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> <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 calling a java bean. Now my problem is how to retrieve the values of the book the user added to their cart in the jsp or java bean. How should I do that .I know I should use the request.parameter(" "), I am not undestanding how ? 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
|
 |
Andy Bowes
Ranch Hand
Joined: Jan 14, 2003
Posts: 171
|
|
You need to have a field on the form that conmtains the ID of the book that you want to add to the cart. Typically this will be a hidden field. When you submit the form this field will be passed as a parameter with the same name as the name of the field. HTH
|
Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
|
 |
Roger Graff
Ranch Hand
Joined: May 29, 2001
Posts: 112
|
|
Originally posted by Archana Annamaneni: 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> <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 calling a java bean. Now my problem is how to retrieve the values of the book the user added to their cart in the jsp or java bean. How should I do that .I know I should use the request.parameter(" "), I am not undestanding how ? 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
You are using the java bean in the wrong place. Once the user has logged in, you want to display an html form to the user. BookSearch.jsp AddToCart.jsp You can find more info about html forms here: http://www.htmlcodetutorial.com/forms/_FORM.html Also, you had used the "<%! %>" tags to declare your UserAccount object. Doing this will cause each user to overwrite the other user's UserAccount object. One JSP page handles requests from many users. The declaration tags above declare that variable to be a JSP's data member variable. Since many user's are sharing the same JSP (servlet) object, they will share that variable. What you really want is to declare User account as a local variable inside the jspService() method (where almost all JSP code goes). You can do this by declaring UserAccount inside the scriptlet tags "<% %>". Lastly, in JSP you can output the value of a variable by using <%= variableName %>. In your example, you were doing:
|
 |
Archana Annamaneni
Ranch Hand
Joined: Jan 29, 2003
Posts: 147
|
|
Hi Thank you all , I got that working. Archana
|
 |
 |
|
|
subject: Strange problem in retrieving the table data in a jsp
|
|
|