File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Using vectors in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Using vectors in servlets" Watch "Using vectors in servlets" New topic
Author

Using vectors in servlets

Davendra Jesani
Greenhorn

Joined: Apr 06, 2005
Posts: 5
Im creating an online e-shop. Im trying to add an item to the basket and display items in the basket using vectors within servlets but it dont seem to work. Does anyone know how it is done?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"How to build a shopping cart" is a bit much to post in a forum.
Have you searched the web for servlet/jsp tutorials.

A lot of books and tutorials use a shopping cart in their examples.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
Vectors work fine in Servlets. there probably is a problem with your implementation. can you post the code?
vasu devan
Ranch Hand

Joined: Apr 14, 2005
Posts: 38
Hi
I think for you there is probelom is session tracking vector object..

paste the codings...
let us find the solutions

cheers
vasu
Davendra Jesani
Greenhorn

Joined: Apr 06, 2005
Posts: 5
Ive managed to add items to the basket using beans. This is the following code:

HttpSession session = request.getSession();
ItemBean bean = new ItemBean();
bean.setProductID(results.getString("ProductID")); bean.setProductDescription(results.getString("ProductDescription")); bean.setProductName(results.getString("ProductName")); bean.setProductPrice(results.getString("ProductPrice"));
bean.setQuantity(Quantity);
ItemBeans item = (ItemBeans)session.getAttribute("basket");
item.addItemBean(bean);

This now works fine. But my next problem is trying use the information from the bean and storing it to the database. Do you guys know how it is done?

Is this how I will go about getting the details?

HttpSession session = request.getSession();
ItemBeans item = (ItemBeans)session.getAttribute("basket");

To get the product name do I do the following?
item.getProductName() or do I use the session attribute basket?

Basket is the id created in the jsp when using the bean.
Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
Davendra Jesani
greenhorn
Member # 96380
posted April 15, 2005 07:36 AM
--------------------------------------------------------------------------------
Ive managed to add items to the basket using beans. This is the following code:

HttpSession session = request.getSession();
ItemBean bean = new ItemBean();
bean.setProductID(results.getString("ProductID")); bean.setProductDescription(results.getString("ProductDescription")); bean.setProductName(results.getString("ProductName")); bean.setProductPrice(results.getString("ProductPrice"));
bean.setQuantity(Quantity);
ItemBeans item = (ItemBeans)session.getAttribute("basket");
item.addItemBean(bean);

This now works fine. But my next problem is trying use the information from the bean and storing it to the database. Do you guys know how it is done?

Is this how I will go about getting the details?

HttpSession session = request.getSession();
ItemBeans item = (ItemBeans)session.getAttribute("basket");

To get the product name do I do the following?
item.getProductName() or do I use the session attribute basket?

Basket is the id created in the jsp when using the bean.


Hi Davendra ,

Are you using the type attribute in JSP(jsp:useBean)??
If yes, then it seems fine...

Regards,
Davendra Jesani
Greenhorn

Joined: Apr 06, 2005
Posts: 5
Hi,

I tried the method of accessing the bean in the servlet but it dont seem to work. It displays an error whenever I try to get an attribute name. For example, item.getProductName() does not work.


Do you know where I am going wrong?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
It displays an error

One place you are going wrong is trying to get help without giving the text of the error. Java error messages can be very informative.
If you are getting a NullPointerException message than one possibility is a difference in the way the parameter is spelled in the form versus the name you are trying to recover.
Bill
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
if you get a ClassnotFoundException for the bean, then here is one possible cause: for some reason, while using the jsp:useBean in JSPs, if the bean class is not part of a package, then the above exception is thrown.
For Ex:- If you have a bean class BeanClass.java in the $TOMCAT_HOME/webapps/<app>/WEB-INF/classes directory and try to access it using jsp:useBean, the above exception may be thrown.

If instead, you put BeaClass.java in a package, say Beans and now try using Beans.BeanClass, that should work.


I know this happens because i have spent too much time figuring it out and had found this issue mentioned somewhere on the internet, but i still dont know why. Can anyone enlighten me on this as well???
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Neeraj Dheer:

I know this happens because i have spent too much time figuring it out and had found this issue mentioned somewhere on the internet, but i still dont know why. Can anyone enlighten me on this as well???



Sun tightened things up a bit in the compiler, starting with J2SE1.4.

From:
http://java.sun.com/j2se/1.4/compatibility.html

The Javac bytecode compiler in J2SE 1.4.0 is more strict than in previous versions in enforcing compliance with the Java Language Specification. The new compiler may not compile existing code that does not strictly conform to the Java Language Specification, even though that code may have compiled with earlier versions of the compiler.

The following are some examples of situations in which the J2SE 1.4.0 compiler is stricter.

The compiler now rejects import statements that import a type from the unnamed namespace. Previous versions of the compiler would accept such import declarations, even though they were arguably not allowed by the language (because the type name appearing in the import clause is not in scope). The specification is being clarified to state clearly that you cannot have a simple name in an import statement, nor can you import from the unnamed namespace.

To summarize, the syntax
import SimpleName;
is no longer legal. Nor is the syntax
import ClassInUnnamedNamespace.Nested;

which would import a nested class from the unnamed namespace. To fix such problems in your code, move all of the classes from the unnamed namespace into a named namespace.

[ April 21, 2005: Message edited by: Ben Souther ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using vectors in servlets