Its all fixed. Problem was ....there was no getAttribute method in Servlet 2.2 API.
New version of Servlet (V 2.2) API replaced getAttribute() method with getValue()
Also it replaced setAttribute() with putValue() method.
Thanks guys,
Reza
Reza U. Nabi wrote:
> Hi J-Gurus,
> I was trying Shopping Cart application form PureJsp book.
> When i compile ShopController.java, got the following error.
> -----------------------------------------------------------------
> classes /com/mednetrix/ShopController.java [27:1] No method found
> mathcing getAttribute(String)
> ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
> classes /com/mednetrix/ShopController.java [27:1] Cannot parse
> initializer
> ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
> 2 errors
> ------------------------------------------------------
>
> I am using JWS 2.0 with Servlet 2.2 API running on Windows NT Server 4.0.
>
> Any point would be just great.......
>
> Thnaks,
>
> Reza Nabi
> [EMAIL PROTECTED]
>
> ********************************************
> BEGIN ShopController.java
> ********************************************
>
> package com.mednetrix;
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.io.*;
> import java.util.*;
>
> import com.mednetrix.ShoppingCart;
>
> public class ShopController extends HttpServlet {
>
> //Initialize global variables
> public void init(ServletConfig config)
> throws ServletException {
>
> super.init(config);
> }
>
> //Process the HTTP Post request
> 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");
>
> // Determine which command to perform
> if ( command.equals("add") ) {
>
> // Get the item from the request
> String id = request.getParameter("id");
> if ( id != null ) {
>
> String desc = request.getParameter("desc");
> Float price = new Float(request.getParameter("price"));
>
> // Add the selected item to the cart
> cart.addItem(id, desc, price.floatValue(), 1);
> }
> }
> // Redirect the response
> // after adding an item to the cart.
> response.sendRedirect("AddToShoppingCart.jsp");
> }
>
> //Get Servlet information
> public String getServletInfo() {
>
> return "ShopController Information";
> }
> }
>
> ********************************************
> END ShopController.java
> ********************************************
>
> ******************************
> ShoppingCart.java
> *******************************
>
> package com.mednetrix;
> import java.lang.String;
> import java.lang.Integer;
> import java.lang.Float;
> import java.util.Hashtable;
> import java.util.Enumeration;
> import java.io.*;
>
> public class ShoppingCart implements Serializable {
>
> protected Hashtable items = new Hashtable();
>
> public ShoppingCart() {
>
> }
>
> /**
> * Add a new item to the shopping cart.
> *
> * attributes
> * itemId - the unique key associted with the item
> * desc - a text description of the item
> * price - the unit price for this item
> * quantity - number of this item to insert into the
> * shopping cart
> */
> 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[] tmpItem = (String[])items.get(itemId);
> int tmpQuant = Integer.parseInt(tmpItem[3]);
> quantity += tmpQuant;
> tmpItem[3] = Integer.toString(quantity);
> }
> else {
>
> items.put(itemId, item);
> }
> }
>
> /**
> * Remove an item from the shopping cart.
> *
> * attributes
> * itemId - the unique key associated with the item to be
> * removed
> */
> public void removeItem(String itemId) {
>
> if (items.containsKey(itemId)) {
>
> items.remove(itemId);
> }
> }
>
> /**
> * Change the quantity of a specific item in the shopping cart.
> * The item must have previously been added to perform this
> * function.
> *
> * attributes
> * itemId - unique key for the item to be updated
> * quantity - the new quantity to be stored in the shopping
> * cart
> */
> public void updateQuantity(String itemId, int quantity) {
>
> if (items.contains(itemId)) {
>
> String[] tmpItem = (String[])items.get(itemId);
> tmpItem[3] = Integer.toString(quantity);
> }
> }
>
> /**
> * Get an Enumeration to the list of items in the shopping cart.
> */
> public Enumeration getEnumeration() {
>
> return items.elements();
> }
>
> /**
> * Get the total cost of all of the items currently in the
> * shopping cart.
> */
> 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 the total number of items currently in the shopping cart.
> */
> 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 ShopingCart.java
> ********************************************
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
--
Reza U. Nabi
Lead Programmer
Mednetrix.com, Incorporated
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets