This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp shopping cart problem

 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

i have an index.jsp page when a customer clicks on a buy button i call additem.jsp passing the isbn code from url.At additem.jsp i take the isbn and find from my database the other columns i need.this is the additem.jsp code...

<%@ page import="Shopping.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page session="true"%>

<%
String productID = request.getParameter("ISBN");
Connection con;
con = null;

try {
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/e_store");
}
catch (ClassNotFoundException e) {
}
catch (SQLException e) {
}
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select ISBN,Title,Price from products where isbn='"+productID+"';");

Hashtable shoppingCart = (Hashtable) session.getAttribute("shoppingCart");
if (shoppingCart==null)
shoppingCart = new Hashtable(20);

try
{
String isbn = rs.getString("ISBN");
String title = rs.getString("Title");
double price = Double.parseDouble(rs.getString("Price"));

out.println(productID);
out.println(title);
if (productID!=null)
{
ShoppingItem item = new ShoppingItem();
item.productId = isbn;
item.productTitle = title;
item.productPrice = price;
item.productQuantity = 1;

shoppingCart.remove(productID);
shoppingCart.put(productID, item);
session.setAttribute("shoppingCart", shoppingCart);
response.sendRedirect("/index.jsp");
}
}
catch (Exception e)
{
out.println("Error adding the selected product to the shopping cart");
}
rs.close();
stmt.close();
%>

but no item is inserted into the cart..
i get as an output the exception i have put...
thnx in advance...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three suggestions:

1) Use UBB code tags when posting code. It will preserve the formatting.

2) Never put Java code in a JSP. Factor it out into beans, or better yet, use a Model 2 controller pattern for your apps.

3) Don't eat the exception. By catching the exception and discarding it, you have lost any information on the nature of the error. Let the excpetion propogate outward so you will receive a stack trace that should give you the details about the error.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually it said null pointer exception and i think it is because it doesnt put anything in shoppingCart!!
its easier for me to understand creating my shopping cart this way..im a begginer so im trying..
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please edit your original post so that the code is properly formatted. Few people are going to look at unformatted code.

The stack trace for the null pointer exception should point out where the exception occurred which is your best clue for tracking it down.

As a beginner is the best time to develop good coding habits and practices.
[ January 08, 2006: Message edited by: Bear Bibeault ]
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my question is why not use the approach i have just posted..if it worked properly too!!!its a school project and finished about everything without the cart,checkout problem..and im stuck..any help will be more than welcome.now it doesnt print anything about an error.it just stays at additem.jsp doing nothing.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aris Doxakis:
my question is why not use the approach i have just posted..if it worked properly too!!!its a school project and finished about everything without the cart,checkout problem..and im stuck..any help will be more than welcome.now it doesnt print anything about an error.it just stays at additem.jsp doing nothing.


You've just given us one good reason for not using this approach.
If you factor all of your database handling code back to a JavaBean, you can test it from the command line to make sure it's working before you start writing the web related or view related code. When, if you're using this approach, something does go wrong, it's usually a lot faster and easier to track it down.

Have you looked in your conatiner's logs to see if any errors are being reported there?
[ January 10, 2006: Message edited by: Ben Souther ]
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you are telling me to call a servlet to do the job insted of calling additem.jsp,that i am doing?Please forgive me but my approach seems a little more easy,not that i am saying it is right,but for me,im a beggine:>,it seems easy.Actually though it doesnt work.Did you find any mistakes in my code?
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's difficult to read all that logic within JSP. That's why you have been adviced to refactor all the DB processing logic to a bean/servlet. Also, that will allow you to test that piece of code outside of the browser.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aris Doxakis:
but for me,im a beggine:>,it seems easy.


At the risk of poking a dead horse with a stick, you've been trying to fix this problem for how long now? Does it seem easy to you?

The point of well-factored code is that it is much easier to understand (therefore eassier to find bugs by inspection), and as Bosun pointed out, much easier to test.

Poorly structured code may seem easier to write, but you're finding out that it's a lot harder to get running correctly. So while it may seem like a waste of time learning how to properly structure your programs up front, you more than make up for it by not having to spend hours and hours chasing frustrating problems.

Claiming that you are a beginner is no excuse for shoddy code practices. In fact, as a beginner is the perfect time to learn and employ accepted best practices and patterns.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually though it doesnt work.

I would start here:
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:

At the risk of poking a dead horse with a stick


I wouldn't dare poking a dead bear with a stick
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:


I wouldn't dare poking a dead bear with a stick


That's relatively safe. But poking a sleeping bear with a stick -- now that's just dangerous!
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Because neither do i want to poke a sleeping bear
i got a book read it and used 3 beans.1 for products,1 for cart item and 1 for cart.and it worked finally..
so thnx for your help guys..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic