aspose file tools*
The moose likes Servlets and the fly likes Unable to import  javabean class in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Unable to import  javabean class in servlet" Watch "Unable to import  javabean class in servlet" New topic
Author

Unable to import javabean class in servlet

Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
Hi all,

I am working out an example of a simple Shopping Cart (MVC architecture)

I wrote Servlet "ShopController.java" in

D:\jakarta-tomcat\webapps\shop\WEB-INF\classes\ShopController.java



I wrote javabean "ShoppingCart.java" in

D:\jakarta-tomcat\webapps\shop\WEB-INF\classes\sho1\sho\ShoppingCart.java


I successfully compiled javabean. But when compiling servlet it is showing errors
to import javabean class..


This is how I imported javabean in my servlet "ShopController.java"

ShopController.java
********************************************************


import sho1.sho.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;


public class ShopController extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
String command =request.getParameter("command");
HttpSession session=request.getSession();
ShoppingCart cart=(ShoppingCart) session.getAttribute("cart");
if(command.equals("add"))
{
String id=request.getParameter("id");
if(id!=null)
{
String desc=request.getParameter("desc");
Float price=new Float(request.getParameter("price"));
cart.addItem(id,desc,price.floatValue(),1);
}
}
response.sendRedirect("/shop/AddToShoppingCart.jsp");
}
public String getServletInfo()
{
return "ShopController information";
}

}

*************************************************************************************

I Wrote Web.xml file as follows.

WEB.XML FILE
*************************************************************************

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app>
<servlet>
<servlet-name>ShopController</servlet-name>
<servlet-class>ShopController</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ShopController</servlet-name>
<url-pattern>/ShopController/*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>ShoppingCart</servlet-name>
<servlet-class>sho1.sho.Shoppingcart</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ShoppingCart</servlet-name>
<url-pattern>/sho1/sho/ShopController/*</url-pattern>
</servlet-mapping>



</web-app>

****************************************************************************


Hope my problem will be solved today with the help of u..


Waiting for ur replies.....
bye.
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Have you declared any package structures in your Bean class?


Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
Hello vijay,

I have declared package structure in my bean class

My javabean "ShoppingCart.java" is as follows

I wrote the javabean "ShoppingCart.java" in
D:\jakarta-tomcat\webapps\shop\sho1\sho\ShoppingCart.java

ShoppingCart.java
***********************************************************
package sho1.sho;

import java.io.Serializable;
import java.lang.String;
import java.lang.Integer;
import java.lang.Float;
import java.util.Enumeration;
import java.util.Hashtable;


public class ShoppingCart implements Serializable
{

protected Hashtable items=new Hashtable();


public ShoppingCart()
{
}


public void addItem(String itemid,String desc,float price,int quantity)
{

String[] item={itemid,desc,Float.toString(price),Integer.toString(quantity)};

if (items .containsKey(itemid))
{
String[] tempitem=(String[])items.get(itemid);
int tempqnt=Integer.parseInt(tempitem[3]);
quantity+=tempqnt;
tempitem[3]=Integer.toString(quantity);
}
else
{

items.put(itemid,item);

}


}


//Remove an item

public void removeItem(String itemid)
{

if (items.containsKey(itemid))
{
items.remove(itemid);
}

}


//update quantity

public void updateqnt(String itemid,int quantity)
{
if (items.contains(itemid))
{
String[] tmpitem=(String[])items.get(itemid);
tmpitem[3]=Integer.toString(quantity);
}
}

//Get enumeration to the list of items

public Enumeration getEnumeration()
{
return items.elements();
}

// get cost


public float getCost()
{

Enumeration enum=items.elements();
String[] tmpitem;
float totalcost=0.00f;

while (enum.hasMoreElements())
{
tmpitem=(String[])enum.nextElement();
totalcost+=(Integer.parseInt(tmpitem[3])*Float.parseFloat(tmpitem[2]));
}
return totalcost;
}


//get total no. of items

public int getNumOfItems()
{

Enumeration enum=items.elements();
String[] tmpitem;
int numofitems=0;

while (enum.hasMoreElements())
{
tmpitem=(String[])enum.nextElement();
numofitems+=Integer.parseInt(tmpitem[3]);
}
return numofitems;
}


}//end of class


**********************************************************


Bye...
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Fine, so your package declarations are correct. Now do one thing. Just check if your ShoppingCart.class is in the sho1.sho package. Or is it somewhere else? The compiler is just not able to locate the ShoppingCart.class file and thats what is causing this error. So please check this once.
Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
hi vijay,

JavaBean "ShoppingCart.java" and its class file "ShoppingCart.class"
are in the same folder

sho1\sho\ShoppingCart.java
sho1\sho\ShoppingCart.class

But the problem is not resolved..

bye
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Wow! This is strange! Whats the error that you are getting exactly? I was assuming it was a class not found error. Can you please post the actual error?
Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
The errors are,

package sho1.sho does not exist
Cannot resolve symbol
symbol : class ShoppingCart
location : class ShopController
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
This is where your mistake is:

I wrote the javabean "ShoppingCart.java" in
D:\jakarta-tomcat\webapps\shop\sho1\sho\ShoppingCart.java


Is this where your class is supposed to be??!!
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Originally posted by Sri Lakshmi:
Hello vijay,

I have declared package structure in my bean class

My javabean "ShoppingCart.java" is as follows

I wrote the javabean "ShoppingCart.java" in
D:\jakarta-tomcat\webapps\shop\sho1\sho\ShoppingCart.java



I am re-formatting my post. I don't think you have placed your ShoppingCart.java in the right place.
Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
Hi ,

I placed javabean "ShoppingCart.java" in the right place...

d:\jakarta-tomcat\webapps\shop\WEB-INF\classes\sho1\sho\ShoppingCart.java

In the previous reply of mine,mistakenly i wrote wrong path...

This is the path Where I wrote my javabean...

Servlet " ShopController.java" is not compiling and giving the above errors...

bye
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Why dont you try setting class path externally. I guess its problem bec of class path.

Regards
Jagdish
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Originally posted by Jagdish Reddy:
Why dont you try setting class path externally. I guess its problem bec of class path.


Yeah. Because there is absolutely nothing wrong with the coding and the package declarations. There could be only two possiblities:

1) There is something wrong with where the class files have been placed.
2) There is something wrong with the classpath.

And I don't think that the WEB-INF directory of Tomcat should be under some custom directory called "shop". Not sure about this one though.
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
As per the explanation I feel nothing wrong with folder structure.
And also I understood the source and class files are at the same place, so I feel the only problem is Class Path.

Regards
Jagdish
Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
Dear Friends,

Following your suggestions, I set my classpath variable in environmental variables as

classpath
d:\jakarta-tomcat-5.0.19\webapps\shop\WEB-INF\classes

but found the same old error.

Note that I restarted my system after setting environmental variable .

Please check my web.xml file . is it correct ?

What should i do to solve the problem

bye.
Mark Stein
Ranch Hand

Joined: May 20, 2002
Posts: 75
I think I see two problems... in your web.xml servlet tag, you have the class name for the bean as Shoppingcart, not ShoppingCart.

Also, why are you even listing the bean in web.xml? It's not a servlet, so it shouldn't be in web.xml.
Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
hi,

I modified WEB.XML file as follows

web.xml
====================================
<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app>
<servlet>
<servlet-name>ShopController</servlet-name>
<servlet-class>ShopController</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ShopController</servlet-name>
<url-pattern>/ShopController/*</url-pattern>
</servlet-mapping>

</web-app>

==================================================

Still getting the same old error...
Is my classpath set correctly?

i set classpath in environmental variables of my system as

d:\jakarta-tomcat-5.0.19\webapps\shop\WEB-INF\classes\sho1\sho

if it is wrong... what should i set to get my program work.

Waiting.....for reply
Mark Stein
Ranch Hand

Joined: May 20, 2002
Posts: 75
You said you placed your ShoppingCart.java file in the right spot? It's the ShoppingCart.class file that you want to put in the directory.
Nikhil Pendharkar
Ranch Hand

Joined: Sep 01, 2003
Posts: 44
Sri ,

I think there is no need to explicitly point the Classpath entry to your application directories .

To include the current directory classes to the Classpath at runtime you have to append just a DOT to the classpath value i.e
CLASSPATH=%CLASSPATH%;.

Also please check if there is more than 1 JRE running or your machine , somtimes this causes strange problems like this . If you have more than 1 JRE installed , remove the non required one .

Also try one more thing , try compiling the Shopping cart java bean from the classes folder itself ,i.e javac sho1\sho\ShoppingCart.java

Hope this helps .
-Nikhil
Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
Friends,

Thanks a lot to you all.

It was indeed the classpath problem. After setting my CLASSPATH correctly I was able to compile the Servlet.

Warm Regards,
SL
[ July 31, 2004: Message edited by: Sri Lakshmi ]
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Hi Sri, my advice to you is dont set the class path in environment. Always try to use ANT to compile classes or atleast write a batch file to compile to classes. Its good practice and also its easy for you to control the build process.

I hope now you are able to run the Servlet :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to import javabean class in servlet