Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JSP Class Problem

0 views
Skip to first unread message

Kevin Docherty

unread,
Jul 4, 2002, 7:24:08 AM7/4/02
to
Hi,

I was wondering if anyone could help.....

I have written a Java bean called ShoppingCart and class called Product for
a shopping cart facility. Both compiled successfully. The code I used (see
below) has
been taken from a book. The page to display the contents of the catalog from
the database loads as normal (this is called DisplayCatalog.jsp). If I
select 'BUY' next to the product, I
get the below message:

500 Internal Server Error
/DisplayCatalog.jsp:

javax.servlet.ServletException: Exception thrown on line '57' from page
'C:\\Program
Files\\Allaire\\JRun\\servers\\default\\default-app\\DisplayCatalog.jsp'.
java.lang.IllegalAccessError: try to access class Product from class
jrun__DisplayCatalog2ejsp13
at
jrun__DisplayCatalog2ejsp13._jspService(jrun__DisplayCatalog2ejsp13.java:104
)
at allaire.jrun.jsp.HttpJSPServlet.service(../jsp/HttpJSPServlet.java:40)
at allaire.jrun.servlet.JRunSE.service(../servlet/JRunSE.java:1013)
at allaire.jrun.servlet.JRunSE.runServlet(../servlet/JRunSE.java:925)
at
allaire.jrun.servlet.JRunNamedDispatcher.forward(../servlet/JRunNamedDispatc
her.java:34)
at allaire.jrun.jsp.JSPServlet.service(../jsp/JSPServlet.java:175)
at allaire.jrun.servlet.JRunSE.service(../servlet/JRunSE.java:1013)
at allaire.jrun.servlet.JRunSE.runServlet(../servlet/JRunSE.java:925)
at
allaire.jrun.servlet.JRunRequestDispatcher.forward(../servlet/JRunRequestDis
patcher.java:88)
at allaire.jrun.servlet.JRunSE.service(../servlet/JRunSE.java:1131)
at allaire.jrun.servlet.JvmContext.dispatch(../servlet/JvmContext.java:330)
at allaire.jrun.http.WebEndpoint.run(../http/WebEndpoint.java:107)
at allaire.jrun.ThreadPool.run(../ThreadPool.java:272)
at allaire.jrun.WorkerThread.run(../WorkerThread.java:75)


Can anyone explain why it doesn't work?

Here is the code:

DisplayCatalog.jsp:

<html>
<head>
<title>Product Catalog</title>
</head>
<body bgcolor="#FFFFFF">
<h3>Product Catalog</h3>
<P align="right"> <a href="ViewCart.jsp">View Shopping Cart</a>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection connection =
java.sql.DriverManager.getConnection("jdbc:odbc:kev", "","");
java.sql.Statement statement = connection.createStatement();
java.sql.ResultSet RS = statement.executeQuery("SELECT * FROM
ProductCatalog");
%>
<table BORDER WIDTH="100%" >
<tr>
<td><b>ProductID</b></td>
<td><b>Title</b></td>
<td><b>Author</b></td>
<td><b>Description</b></td>
<td><b>Price</b></td>
<td> </td>
</tr>
<%
while(RS.next())
{
String ProductID = RS.getString("ProductID");
String Title = RS.getString("Title");
String Author = RS.getString("Author");
String Description = RS.getString("Description");
String Price = RS.getString("Price");
%>

<tr>
<td><%=ProductID %></td>
<td><%=Title %></td>
<td><%=Author %></td>
<td><%=Description %></td>
<td>$<%=Price %></td>
<td><A HREF="DisplayCatalog.jsp?title=<%=Title %>&id=<%=ProductID %>&price=
<%=Price %>">BUY</A></td>
</tr>

<%
}
RS.close();
connection.close();
%>
</table>

<jsp:useBean id="sCart" class="ShoppingCart" scope="session"/>
<%@ page import="Product" %>
<% String title = request.getParameter("title");
if(title!=null)
{
String id = request.getParameter("id");
double price = Double.parseDouble(request.getParameter("price"));
Product newProduct = new Product(id, title, price);
sCart.addProduct(newProduct);
}
%>

</body>
</html>

Here is the Product Class:

public class Product
{
String id, title;
int quantity = 1;
double price, total;

public Product(){}

public Product(String newid, String newtitle, double newprice)
{
id= newid;
title = newtitle;
price = newprice;
}

public String getId()
{
return id;
}

public String getTitle()
{
return title;
}

public int getQuantity()
{
return quantity;
}

public double getPrice()
{
return price;
}

public double getTotal()
{
return (price*quantity);
}
}


Here is the JavaBean:

import java.util.*;

public class ShoppingCart
{
Vector products = new Vector();

public void addProduct(Product i)
{
boolean productFound = false;
Enumeration productEnum = getProducts();
while(productEnum.hasMoreElements())
{
Product product = (Product)productEnum.nextElement();
if(product.getId().equals(i.id))
{
productFound = true;
product.quantity += 1;
break;
}
}
if (!productFound)
{
products.addElement(i);
}
}

public void deleteProduct(String id)
{
Enumeration productEnum = getProducts();
while(productEnum.hasMoreElements())
{
Product product = (Product)productEnum.nextElement();
if(product.getId().equals(id))
{
products.removeElement(product);
break;
}
}
}

public void emptyCart()
{
products = new Vector();
}

public int getNoProducts()
{
return products.size();
}

public Enumeration getProducts()
{
return products.elements();
}

public double getTotal()
{
Enumeration productEnum = getProducts();
double total = 0;
while(productEnum.hasMoreElements())
{
Product product = (Product)productEnum.nextElement();
total = total + product.getTotal();
}
return total;
}
}

0 new messages