• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strange problem in retrieving the table data in a jsp

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thank you all , I got that working.
Archana
 
reply
    Bookmark Topic Watch Topic
  • New Topic