aspose file tools*
The moose likes JSP and the fly likes jsp shopping cart problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp shopping cart problem" Watch "jsp shopping cart problem" New topic
Author

jsp shopping cart problem

Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

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

Joined: Jan 10, 2002
Posts: 61226
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

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

Joined: Jan 10, 2002
Posts: 61226
    
  66

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

Joined: Dec 05, 2004
Posts: 136

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

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

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

Joined: Nov 06, 2000
Posts: 1510
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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

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

Joined: Dec 11, 2004
Posts: 13410

Actually though it doesnt work.

I would start here:
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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


[My Blog]
All roads lead to JavaRanch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

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

Joined: Dec 05, 2004
Posts: 136

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..
 
wood burning stoves
 
subject: jsp shopping cart problem