We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

javax.servlet.ServletException: shop/ShoppingBasket (wrong name: ShoppingBasket)

APPIREDDY
APPIREDDY asked
on
Medium Priority
1,010 Views
Last Modified: 2013-11-24
Hi experts,
I am using jsp and tomcat to develop a shopping cart appln,
I have created two java ben classes ShoppingBasket,Product and placed them in
C:\Tomcat 5.5\webapps\ROOT\shop\WEB-INF\classes
I have included this directory to the classpath, but when i am accessing the bean from jsp i'm getting the following error
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

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

exception

javax.servlet.ServletException: shop/ShoppingBasket (wrong name: ShoppingBasket)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:272)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.NoClassDefFoundError: shop/ShoppingBasket (wrong name: ShoppingBasket)
      java.lang.ClassLoader.defineClass1(Native Method)
      java.lang.ClassLoader.defineClass(Unknown Source)
      java.security.SecureClassLoader.defineClass(Unknown Source)
      org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1650)
      org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:856)
      org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1305)
      org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1187)
      org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1210)
      org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
      org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
      org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
      org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
      org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
      org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
      org.apache.jasper.compiler.Generator.generate(Generator.java:3284)
      org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:189)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


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


--------------------------------------------------------------------------------

Apache Tomcat/5.5.12

my jsp code is

<%@ page language="java" contentType="text/html" import="shop.shoppingBasket,shop.Product" errorPage="errorpage.jsp" %>
<html>
<head><title>Welcome to the shop </title></head>
<body>
<table width="385" border="0" cellspacing="0">
<tr><td colspan="4">More books from Mike McGrath:</td></tr>
<tr><td colspan="4" align="right">
<a href="<%= response.encodeURL("shop-basket.jsp")%>">
<img src="images\viewbasket.gif"></a></td></tr>
<tr>
<td><b>Ref</b></td><td><b>Title</b></td>
<td><b>Price</b></td><td></td></tr>
 
<% Class.forName("com.mysql.jdbc.Driver"); 
   java.sql.Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop","anu","kodanda");
   System.out.println("successful");
   java.sql.Statement stmt = connection.CreateStatement();
   java.sql.ResultSet RS = stmt.executeQuery("select * from items");
   
   int rowCounter=0;
   while(Rs.next())
   {
	   String item_id = Rs.getString("item_id");
	   String title = Rs.getString("title");
	   String description = Rs.getString("description");
	   String price = Rs.getString("price");
	   rowCounter++;
	   String bg =(rowCounter % 2 !=0)?"#C0C0C0":"FFFFFF";
>%
	   <tr bgcolor="<%= bg %>">
	   <td><%= item_id %></td>
	   <td><b><%= title %><b><br/><%=description %></td>
	   <td>£<%=price %></td>
	   <td>
	   <a href="<%= response.encodeUrl("shop-products.jsp?title="+title+"&item_id="+item_id+"&price="+price)%>">
	   <img src ="images\addtobasket.gif"><a></td>
	   </tr>
<%  }RS.close(); connection.close();%>
</table>
<jsp:useBean id="basket" class="shop.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>
 
pls suggest me where I am wrong
thanks

Open in new window

Comment
Watch Question

rrzstudent
CERTIFIED EXPERT

Commented:
>I have created two java ben classes ShoppingBasket,Product and placed them in
C:\Tomcat 5.5\webapps\ROOT\shop\WEB-INF\classes
If the first line in their source files is
package shop;
then put them into a folder called shop  and put that folder into  C:\Tomcat 5.5\webapps\ROOT\shop\WEB-INF\classes
>then put them into a folder called shop  and put that folder into  C:\Tomcat 5.5\webapps\ROOT\shop\WEB-INF\classes

I think rrz meant

  C:\Tomcat 5.5\webapps\ROOT\WEB-INF\shop\classes ( assuming your context for the web app is in root).
rrzstudent
CERTIFIED EXPERT

Commented:
Hi kuldeep, Thanks for the help.  But, no, that is not what I meant.  
I meant
C:\Tomcat 5.5\webapps\ROOT\shop\WEB-INF\classes\shop\ShoppingBasket.class
Yup I read your statement again.. create then in folder shop and put that "folder"...

thats what I missed while reading it..:)

Author

Commented:
thankyou rrz,kuladeep
i'm getting a syntax error at line 14
i don't understand what it is

An error occurred at line: 14 in the jsp file: /shop/shop-products.jsp
Generated servlet error:
Syntax error on tokens, delete these tokens

can you help me
can you post the full stacktrace of the error.. while looking at your code above one thing that I can right away see is that you are declaring you resultset as "RS" and trying to use it as "Rs"...

Author

Commented:
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: Unable to compile class for JSP

An error occurred at line: 15 in the jsp file: /shop/shop-products.jsp
Generated servlet error:
Syntax error on tokens, delete these tokens


      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:409)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


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


--------------------------------------------------------------------------------

Apache Tomcat/5.5.12

Author

Commented:
i have changed Rs to RS still the same
Interesting..

do you have any comments or something like that? normally the error above comes when there are unclosed comments tags and things like that..

Also is that the full code of your jsp thats posted above?

Author

Commented:
there is no comment in the code i'm  attaching the code again

<%@ page language="java" contentType="text/html"  errorPage="errorpage.jsp" %>
<%@ page import = "shop.*" %>
<html>
<head><title>Welcome to the shop </title></head>
<body>
<table width="385" border="0" cellspacing="0">
<tr><td colspan="4">More books from Mike McGrath:</td></tr>
<tr><td colspan="4" align="right">
<a href="<%= response.encodeURL("shop-basket.jsp")%>">
<img src="images\viewbasket.gif"></a></td></tr>
<tr>
<td><b>Ref</b></td><td><b>Title</b></td>
<td><b>Price</b></td><td></td></tr>
 
<% Class.forName("com.mysql.jdbc.Driver"); 
   java.sql.Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop","anu","kodanda");
   System.out.println("successful");
   java.sql.Statement stmt = connection.CreateStatement();
   java.sql.ResultSet RS = stmt.executeQuery("select * from items");
   
   int rowCounter=0;
   while(RS.next())
   {
	   String item_id = RS.getString("item_id");
	   String title = RS.getString("title");
	   String description = RS.getString("description");
	   String price = RS.getString("price");
	   rowCounter++;
	   String bg =(rowCounter % 2 !=0)?"#C0C0C0":"FFFFFF";
>%
	   <tr bgcolor="<%= bg %>">
	   <td><%= item_id %></td>
	   <td><b><%= title %><b><br/><%=description %></td>
	   <td>£<%=price %></td>
	   <td>
	   <a href="<%= response.encodeUrl("shop-products.jsp?title="+title+"&item_id="+item_id+"&price="+price)%>">
	   <img src ="images\addtobasket.gif"><a></td>
	   </tr>
<%  }RS.close(); connection.close();%>
</table>
<jsp:useBean id="basket" class="shop.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>

Open in new window

sometimes its little silly items that kill it..:)

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


needs to be

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

Notice the location of the % sign

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
rrzstudent
CERTIFIED EXPERT
Commented:
>         String bg =(rowCounter % 2 !=0)?"#C0C0C0":"FFFFFF";
That line should compile.  Just needs a little help.
String bg =(rowCounter % 2 !=0)?"#C0C0C0":"#FFFFFF";
It is the next line after that one that looks bad to me.
29:>%
shouldn't that be  
29:%>

>An error occurred at line: 15 in the jsp file: /shop/shop-products.jsp
Which is line 15 anyway?  
I believe RRZ also helped in this problem, so he also needs to share the points with me.
rrzstudent
CERTIFIED EXPERT

Commented:
thanks kuldeep, I appreciate that
rrzstudent
CERTIFIED EXPERT

Commented:
I requested the attention of an adviser.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.