importing classes in JSP help.

Rahul++

SuperUser
Skilled
Hey guys.. I'm working on one project which is in directory -

Quote ...
C:\Tomcat 6\webapps\ROOT\cart
This is the code in file called shop.jsp



Code:
<%@ page language = "java" contentType = "text/html"

         import = "ShoppingBasket, Product, java.sql.*"

         errorPage = "errorpage.jsp" %>

<html> 

<head> <title>Welcome to Shop</title></head>

<body>

<table width = "385" border="0" cellspacing="0">

<tr>

<td colspan="4">More Books from me</td>

</tr><tr>

<td colspan="4" align ="right">

<a href = "<%=response.encodeURL("shop-basket.jsp") %>"> View Basket </a></td>

</tr><tr>

<td>[b]Ref[/b]</td>[b]Title[/b]</td>

<td>[b]Price[/b]</td></td></tr>

<%

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

Connection connection = null;

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "rahul");

Statement stmt = null;

ResultSet RS = null;

stmt = connection.createStatement();

RS = stmt.executeQuery("select * from items");

int rowCounter = 0;

    while(RS.next())

    {

      String item_id = RS.getString("id");

      String title = RS.getString("title");

      String desc = RS.getString("desc");

      String price = RS.getString("price");

      rowCounter=rowCounter+1;

      String bg=  (rowCounter % 2 !=0) ?"#C0C0C0":"#ffffff";

%>

<tr bgcolor = <%=bg%>">

<td><%=item_id%></td>

<td>[b]<%=title %>[/b]
<%=desc%></td>

<td>[b]<%=price %>[/b]</td>

<td>

<a href =" <% response.encodeURL("shop-products.jsp?title="+title+"&item_id="+item_id+"&price="+price); %> "> Add to Cart </a></td>

</tr>

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

</table>

<jsp:useBean id = "basket" class = "ShoppingBasket" scope = "session"/>

<% String title = request.getParameter("title");

   if(title!=null)

   {

     String item_id = request.getParameter("item_id");

     double price = Double.parseDouble(request.getParameter("price"));

     Product item = new Product (item_id, title, price);

     basket.addProduct(item);

   }

%>

</body></html>

I've to include class files named ShoppingBasket and Product but somehow I'm not getting where to put those class files so that my JSP file will automatically include them.

As the classes are not included, I'm getting errors like

Code:
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 6 in the generated java file

The import ShoppingBasket cannot be resolved

An error occurred at line: 7 in the generated java file

The import Product cannot be resolved

An error occurred at line: 62 in the jsp file: /cart/shop-products.jsp

The method addProduct(Product) in the type ShoppingBasket is not applicable for the arguments (Product)

59:      String item_id = request.getParameter("item_id");

60:      double price = Double.parseDouble(request.getParameter("price"));

61:      Product item = new Product (item_id, title, price);

62:      basket.addProduct(item);

63:    }

64: %>

65: </body></html>

Please help me

Thanks,

Rahul
 
A word of advice .

If you are in hurry or need something like quick reply just register on sun official forum and copy past the post there you will get a reply in 10 min(personal exp may vary.)

No hard feelings for TE guineas.
 
Rahul, I might be able to help but need to know some things first:

Does your class ShoppingBasket belong to any package?

What's the CLASSPATH being set to on server startup?
 
Bandu said:
Rahul, I might be able to help but need to know some things first:

Does your class ShoppingBasket belong to any package?

What's the CLASSPATH being set to on server startup?

^ The class files ShoppingBasket and Product are separate classes and are not declared in any Package.

I didn't got your second question, can you please explain? How to check which classpath is being set on server startup?
 
Hi Rahul,

1. So, there is no package declaration in your classes. OK.

2. Check your server startup file. I've not used tomcat, but there should be some .bat file to start it up. Look for something by the name run.bat and run.conf in the TOMCAT_HOME/bin directory. There should be some line in there which says SET CLASSPATH=... Add echo %CLASSPATH% after this line. Doing this will print the CLASSPATH on server startup.

Since you confirmed that you are not using any packages, try placing your .class files in the lib folder of your app. I guess it would be C:\Tomcat 6\webapps\ROOT\cart\lib

Let me know if this works.
 
Back
Top