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

C:\Tomcat 6\webapps\ROOT\cart

This is the code in file called shop.jsp

<%@ 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><br/><%=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

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

Please help me :)

Thanks,
Rahul

Recommended Answers

All 12 Replies

The JSP import page directive expects classes to be declared in the form "package.class", e.g.
<%@ page import = "mypackage.MyClass"%>

So, do a bit of refactoring and put your classes under a package.
It's good programming practice anyway...

Also make sure your .class files (with their respective folder hierarchy) are under the folder WEB-INF/classes of your web application. Or, if you have packaged them in a jar, put it in the folder WEB-INF/lib.

And a tip...move all your Java code where it belongs, in Java classes. Not only to improve JSP readability, but also to make it reusable by other JSPs.
Keep your DB connectivity and business logic as seperate as possible from view (JSP).

Here is what I did now -
Changed my app path to -
C:\Tomcat 6\webapps\shop

Now, I've created folder WEB-INF inside my \shop application.. Inside WEB-INF I created 2 folders \lib and \classes respectively. I copied the ShoppingBasket.class and Product.class files into \classes\shop folder.

I imported it in jsp file using -
<%@ page import = "shop.*"%>

Still I'm getting error -

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /shop-products.jsp(55,0) The value for the useBean class attribute ShoppingBasket is invalid.
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1229)
    org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1178)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2417)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:495)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
    org.apache.jasper.compiler.Generator.generate(Generator.java:3440)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:231)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:347)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.

When I refresh the page again, the error changes to -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to load class for JSP
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:156)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

org.apache.jasper.JasperException: Unable to load class for JSP
    org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:630)
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:144)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.lang.ClassNotFoundException: org.apache.jsp.shop_002dproducts_jsp
    java.net.URLClassLoader$1.run(Unknown Source)
    java.security.AccessController.doPrivileged(Native Method)
    java.net.URLClassLoader.findClass(Unknown Source)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:134)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
    org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:628)
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:144)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.

I copied the ShoppingBasket.class and Product.class files into \classes\shop folder.

You cannot just simply copy and paste existing as you wish specially if these classes have package declaration at start of file. Here is and example how to do it from command line, if you using IDE (IntelliJ, Eclips, NetBeans) and created your project correctly they would do whole compile and deploy process for you. Nevertheless it is always good to know how to compile from command line

As peter said just moving your files around is not enough. The compiler really cares for the package your classes belong to, not the folder.

So make sure you have added the package declaration in the beginning of your classes' source file ("package shop;").

Also, it would indeed be good to use an IDE for writing and refactoring your code. Such problems don't occur this way!

So make sure you have added the package declaration in the beginning of your classes' source file ("package shop;").

Not true if you want to keep your files in top level, meaning classes folder. At that time you do not need package declarations.

I've compiled the ShoppingBasket.java and Product.java files separately which include add, remove and things that are required for Shop Cart thing. .class file are created for both. As per I've learned, I just have to paste them in appropriate folders to get imported inside my jsp. If I'm not correct can you guys please tell me where to copy those .class files or how to compile in proper way?

If I'm not correct can you guys please tell me where to copy those .class files or how to compile in proper way?

Already done in my first reply, obviously you did not checked that link...

Not true if you want to keep your files in top level, meaning classes folder. At that time you do not need package declarations.

...which unfortunately is a really bad idea in case anyone reading this thread thinks that it would be a convenient one. As of Java 1.4, unnamed namespaced classes can't be imported by namespaced classes i.e. are effectively invisible to namespaced classes.

@s.o.s, good point nevertheless beginners are unlikely to understand it ;)

Not true if you want to keep your files in top level, meaning classes folder. At that time you do not need package declarations.

Of course you are right.
I'm sorry if I was not clear...I was referring to the aforementioned (non-top level) classes of package shop, which were supposed to be moved to a folder with the same name.

...which unfortunately is a really bad idea in case anyone reading this thread thinks that it would be a convenient one. As of Java 1.4, unnamed namespaced classes can't be imported by namespaced classes i.e. are effectively invisible to namespaced classes.

...which is indeed the case with the JSP compiler and the Java class it generates, resulting to the error of the original post.

...which is indeed the case with the JSP compiler and the Java class it generates, resulting to the error of the original post.

Did you actually try it since you making this statement? I do not think so...

Did you actually try it since you making this statement? I do not think so...

I assure you I never post without having tested first. In this case in JDev 11 with JSP 2.1/Servlet 2.5.
Please explain where you think is the fault in that statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.