?
Solved

I am getting a runtime error when I deploy my JSP to tomcat

Posted on 2003-03-08
43
Medium Priority
?
5,601 Views
Last Modified: 2013-11-24
My project consists of 3 jsps:  DVDproduct.jsp, DVDcatalog.jsp, and DVDaddtocart.jsp......
The DVDcatalog.jsp works but DVDproduct.jsp and DVDaddtocart.jsp are both getting the same error of:
--------------------------------------------------------------------------------------------------------------
Error: 500
Location: /jsp/DVDaddtocart.jsp
Internal Servlet Error:

javax.servlet.ServletException: A property getter for product not found in catalog

Root cause:
javax.servlet.jsp.JspException: A property getter for product not found in catalog
      at dvdshop.tags.UsePropertyTag.getProperty(UsePropertyTag.java:140)
      at dvdshop.tags.UsePropertyTag.doEndTag(UsePropertyTag.java:94)
---------------------------------------------------------------------------------------------------------------

I know that the DVDCatalogBean.java (which is the file that DVDaddtocart.jsp and DVDproduct.jsp are trying to access) is fine because I ran a test on it...but I still can't

find the problem.

UsePropertyTag.java or the jsp's are the only files may be a problem but I don't see anything wrong with it.

thanks,
Warren


Below are the copies of the code:    

_____________________________________________________________________________________________________________________
DVDaddtocart.jsp:
___________________________________________________

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html" %>
<%@ taglib uri="dvdtaglib.tld" prefix="dvd" %>
<%@ page import="java.text.*" %>
<%@ page import="dvdshop.DVDProductBean" %>
<%@ page import="dvdshop.DVDCatalogBean" %>
<%@ page import="dvdshop.DVDCartBean" %>

<html>

<HEAD>
  <META name="GENERATOR" content="IBM WebSphere Studio">
</HEAD>


<body>

 hi

  <jsp:useBean
  id="catalog"
  scope="application"
  class="dvdshop.DVDCatalogBean"
  />

  <jsp:useBean
  id="cart"
  scope="session"
  class="dvdshop.DVDCartBean"
  />

  <%-- Get the DVDProductBean from the catalog and save it in the cart --%>
  <dvd:useProperty id="product" name="catalog" property="product"
  arg='<%= request.getParameter("id") %>'
  className="dvdshop.DVDProductBean" />

  <jsp:setProperty name="cart" property="product" value="<%= product %>" />

  <%-- Redirect back to the catalog page --%>
  <dvd:redirect page="DVDcatalog.jsp" />
 
</body>
 
</html>
_________________________________________________________________________________________________________________
DVDproduct.jsp:
_____________________________________________________

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html" %>
<%@ taglib uri="dvdtaglib.tld" prefix="dvd" %>
<%@ page import="java.text.*" %>
<%@ page import="dvdshop.DVDProductBean" %>


<html>
 
  <head>
    <title>Product Description</title>
  <META name="GENERATOR" content="IBM WebSphere Studio">
  </head>

  <body bgcolor="white">

    <jsp:useBean id="catalog" scope="application" class="dvdshop.DVDCatalogBean" />

    <%-- Get the DVDProductBean from the catalog --%>
    <dvd:useProperty id="product" name="catalog" property="product"
      arg="<%= request.getParameter(\"id\") %>"
      className="dvdshop.DVDProductBean" />

    <h1>
      <jsp:getProperty name="product" property="name" />
    </h1>

    <jsp:getProperty name="product" property="descr" />

    <p>
    <a href="<dvd:encodeURL url="DVDaddtocart.jsp">

             </dvd:encodeURL>">Add this book to the shopping cart</a>

  </body>
</html>
_____________________________________________________________________________________________________________________
DVDcatalog.jsp
____________________________________________________
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html" %>
<%@ taglib uri="dvdtaglib.tld" prefix="dvd" %>

<%@ page import="java.text.*" %>
<%@ page import="dvdshop.DVDProductBean" %>
<%@ page import="dvdshop.DVDCatalogBean" %>
<%@ page import="dvdshop.DVDCartBean" %>


<html>
 
 <head>
    <title>Warren's DVD Store Inc</title>
  <META name="GENERATOR" content="IBM WebSphere Studio">
 </head>
 
 <body bgcolor="white">
   
 <table width="100%" height="63" border=0 cellpadding=4 cellspacing=0>
  <tr bgcolor=9bbad6><td valign=top colspan=4>
  <font face=arial size=20>
  <b>
  <font
  size="+3" face="Arial, Helvetica, sans-serif">DVD Store Catalog
  </font>
  </b>
  </tr>
 </table>

<br>
    Please choose a dvd from our catalog to read the description and
    decide if you like to purchase a copy:

    <jsp:useBean
      id="catalog"
      scope="application"
      class="dvdshop.DVDCatalogBean"
    />

  <%-- Generate a list of all products with links to the product page. --%>
   
    <ul>
      <dvd:loop name="catalog" property="productList" loopId="product" className="DVDProductBean">
         <li>
          <a href="
           <dvd:encodeURL url="DVDproduct.jsp ">
                 <dvd:param name="id" value="<%= product.getId() %>"/>
               </dvd:encodeURL>"><%= product.getName() %>
          </a>
      </dvd:loop>
    </ul>

    <jsp:useBean
      id="cart"
      scope="session"
      class="dvdshop.DVDCartBean"
    />

    <%-- Show the contents of the shopping cart, if any --%>
    <%
       if (!cart.isEmpty()) {
         NumberFormat numFormat = NumberFormat.getCurrencyInstance();
    %>
         Your shopping cart contains the following items:
         <p>
         <table border=0>

           <dvd:loop name="cart" property="products" loopId="product" className="DVDProductBean">
             <tr>
               <td><%= product.getName() %></td>
               <td><%= numFormat.format(product.getPrice()) %></td>
             </tr>
           </dvd:loop>

           <tr><td colspan=2><hr></td></tr>
           <tr>
             <td><b>Total:</b></td>
             <td><%= numFormat.format(cart.getTotal()) %></td></tr>
        </table>
    <% } %>

  </body>

</html>

___________________________________________________________________________________________________________________
dvdtaglib.tld
____________________________________________
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
     PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"         "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
  <tlibversion>1.0</tlibversion>
  <jspversion>1.1</jspversion>
  <shortname>dvd</shortname>
  <uri>/dvdtaglib</uri>
 

  <tag>
    <name>param</name>
    <tagclass>dvdshop.tags.ParamTag</tagclass>
    <bodycontent>JSP</bodycontent>
    <description>
      Adds a parameter to a containing 'import' tag's URL.
    </description>
    <attribute>
        <name>name</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>value</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>


  <tag>

    <name>loop</name>
    <tagclass>dvdshop.tags.LoopTag1</tagclass>
    <teiclass>dvdshop.tags.LoopTag1ExtraInfo</teiclass>
    <bodycontent>JSP</bodycontent>
   
    <attribute>
      <name>name</name>
      <required>true</required>
    </attribute>

    <attribute>
      <name>property</name>
      <required>false</required>
    </attribute>

    <attribute>
      <name>loopId</name>
      <required>true</required>
    </attribute>

    <attribute>
      <name>className</name>
      <required>true</required>
    </attribute>

  </tag>



 






  <tag>
    <name>encodeURL</name>
    <tagclass>dvdshop.tags.EncodeURLTag</tagclass>
    <bodycontent>JSP</bodycontent>
    <info>
      Encodes the url attribute and possible param tags in the body
    </info>

    <attribute>
      <name>url</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
 



  <tag>
    <name>redirect</name>
    <tagclass>dvdshop.tags.RedirectTag</tagclass>
    <bodycontent>JSP</bodycontent>
    <info>
      Encodes the url attribute and possible param tags in the body
      and set redirect headers.
    </info>

    <attribute>
      <name>page</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>

  </tag>



  <tag>
    <name>useProperty</name>
    <tagclass>dvdshop.tags.UsePropertyTag</tagclass>
    <teiclass>dvdshop.tags.UsePropertyTagExtraInfo</teiclass>
    <bodycontent>JSP</bodycontent>
    <info>
      Gets a bean property using a getter with a String argument
    </info>

    <attribute>
      <name>id</name>
      <required>true</required>
    </attribute>

    <attribute>
      <name>name</name>
      <required>true</required>
    </attribute>

    <attribute>
      <name>property</name>
      <required>true</required>
    </attribute>

    <attribute>
      <name>arg</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>

    <attribute>
      <name>className</name>
      <required>true</required>
    </attribute>

  </tag>

</taglib>
___________________________________________________________________________________________________________________

EncodeURLTag.java
_______________________________
package dvdshop.tags;

import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
 * This class is a custom action for encoding URLs for URL rewriting
 * (session tracking) with possible parameter values URL encoded.
 * @tagName encodeURL
 * @displayName <{EncodeURLTag}>
 * @persistent
 * @WebService
 *
 */
public class EncodeURLTag extends TagSupport implements ParamParent {
    private String url;
    private Vector params;


    /**
     * Sets the url attribute.
     *
     * @param url the page URL value
     */
    public void setUrl(String url) {
        this.url = url;
    }
   
    /**
     * Adds a parameter name and value. This method is called by param
     * tags in the action body.
     *
     * @param name the parameter name
     * @param value the URL encoded parameter value
     */
    public void addParameter(String name, String value) {
        if (params == null) {
            params = new Vector();
        }
        Param param = new Param(name, value);
        params.addElement(param);
    }
 
    /**
     * Override the default implementation so that possible
     * param actions in the body are processed.
     */
    public int doStartTag() {
        // Reset per-use state set by nested elements
        params = null;

        return EVAL_BODY_INCLUDE;
    }
   
    /**
     * Appends possible URL encoded parameters to the main URL,
     * encodes the result for URL rewriting and writes the result
     * to the JspWriter.
     */
    public int doEndTag() throws JspException {
        StringBuffer encodedURL = new StringBuffer(url);
        if (params != null && params.size() > 0) {
            encodedURL.append('?');
            boolean isFirst = true;
            Enumeration e = params.elements();
            while (e.hasMoreElements()) {
                Param p = (Param) e.nextElement();
                if (!isFirst) {
                    encodedURL.append('&');
                }
                encodedURL.append(p.getName()).append('=').
                    append(p.getValue());
                isFirst = false;
            }
        }
        try {
            HttpServletResponse res =
                (HttpServletResponse) pageContext.getResponse();
            JspWriter out = pageContext.getOut();
            out.print(res.encodeURL(encodedURL.toString()));
        }
        catch (IOException e) {}
        return EVAL_PAGE;
    }

    /**
     * Releases all instance variables.
     */
    public void release() {
        url = null;
        params = null;
        super.release();
    }
   
    /**
     * This is a helper class that holds the name and value of a
     * parameter.
     */
    class Param {
        private String name;
        private String value;
       
        public Param(String name, String value) {
            this.name = name;
            this.value = value;
        }
       
        public String getName() {
            return name;
        }
       
        public String getValue() {
            return value;
        }
    }
}


__________________________________________________________________________________________________________________
ParamParent.java
_________________________________
package dvdshop.tags;

/**
 * <p>Interface for tag handlers implementing valid parent tags for
 * &lt;c:param&gt;.</p>
 *
 * @author Shawn Bayern
 */

public interface ParamParent {

    /**
     * Adds a parameter to this tag's URL.  The intent is that the
     * &lt;param&gt; subtag will call this to register URL parameters.
     * Assumes that 'name' and 'value' are appropriately encoded and do
     * not contain any meaningful metacharacters; in order words, escaping
     * is the responsibility of the caller.
     *
     * @see ParamSupport
     */
    void addParameter(String name, String value);
   
}
____________________________________________________________________________________________________________________
ParamSupport.java
__________________________________


package dvdshop.tags;

import java.lang.reflect.*;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.net.URLEncoder;

import java.lang.*;






/**
 * <p>Support for tag handlers for &lt;param&gt;, the URL parameter
 * subtag for &lt;import&gt; in JSTL 1.0.</p>
 *
 * @see ParamParent, ImportSupport, URLEncodeSupport
 * @author Shawn Bayern
 */

public abstract class ParamSupport extends BodyTagSupport {

    //*********************************************************************
    // Private constants

    private static final Class[] URL_ENCODER_PARAM_TYPES =
      new Class[] { String.class, String.class };

    //*********************************************************************
    // Protected state

    protected String name;                       // 'name' attribute
    protected String value;                      // 'value' attribute

    /**
     * There used to be an 'encode' attribute; I've left this as a
     * vestige in case custom subclasses want to use our functionality
     * but NOT encode parameters.
     */
    protected boolean encode = true;

    //*********************************************************************
    // Private state

    private static Method encodeMethod1_4 = null;

    //*********************************************************************
    // Constructor and initialization

    // URLEncoder.encode(String) has been deprecated in J2SE 1.4.
    // Take advantage of the new method URLEncoder.encode(String, enc)
    // if J2SE 1.4 is used.
    static {
      try {
          Class urlEncoderClass = Class.forName("java.net.URLEncoder");
          encodeMethod1_4 =
                urlEncoderClass.getMethod(
                    "encode",
                new Class[] {String.class, String.class});
        } catch (Exception ex) {} // encodeMethod1_4 will be null if exception
    }

    public ParamSupport() {

      super();
      init();
    }

    private void init() {
      name = value = null;
    }


    //*********************************************************************
    // Tag logic

    // simply send our name and value to our appropriate ancestor
    public int doEndTag() throws JspException {
      Tag t = findAncestorWithClass(this, ParamParent.class);
      if (t == null)
          throw new JspTagException(
            Resources.getMessage("PARAM_OUTSIDE_PARENT"));

      // take no action for null or empty names
      if (name == null || name.equals(""))
          return EVAL_PAGE;

      // send the parameter to the appropriate ancestor
      ParamParent parent = (ParamParent) t;
      String value = this.value;
      if (value == null) {
          if (bodyContent == null || bodyContent.getString() == null)
            value = "";
          else
            value = bodyContent.getString().trim();
      }
      if (encode) {
            if (encodeMethod1_4 != null) {
              Object[] methodArgsName = new Object[2];
              methodArgsName[0] = name;
              methodArgsName[1] = pageContext.getResponse().getCharacterEncoding();
              Object[] methodArgsValue = new Object[2];
              methodArgsValue[0] = value;
              methodArgsValue[1] = pageContext.getResponse().getCharacterEncoding();
              
                try {
                    parent.addParameter(
                    (String)encodeMethod1_4.invoke(null, methodArgsName),
                    (String)encodeMethod1_4.invoke(null, methodArgsValue));  
                } catch (Exception ex) {
                    throw new JspException("System error invoking URLEncoder.encode() by reflection.");
                }
            } else {
                // must use J2SE 1.3 version
              parent.addParameter(
                URLEncoder.encode(name), URLEncoder.encode(value));
            }
      } else {
          parent.addParameter(name, value);
        }
      return EVAL_PAGE;
    }

    // Releases any resources we may have (or inherit)
    public void release() {
      init();
    }

    //*********************************************************************
    // Support for parameter management

    /**
     * Provides support for aggregating query parameters in URLs.
     * Specifically, accepts a series of parameters, ensuring that
     *  - newer parameters will precede older ones in the output URL
     *  - all supplied parameters precede those in the input URL
     */
    public static class ParamManager {

        //*********************************
        // Private state

      private List names = new LinkedList();
        private List values = new LinkedList();
      private boolean done = false;
       
      //*********************************
        // Public interface

      /** Adds a new parameter to the list. */
        public void addParameter(String name, String value) {
          if (done)
            throw new IllegalStateException();
          if (name != null) {
              names.add(name);
              if (value != null)
                values.add(value);
              else
                values.add("");
          }
      }

      /**
         * Produces a new URL with the stored parameters, in the appropriate
         * order.
         */
      public String aggregateParams(String url) {
          /*
             * Since for efficiency we're destructive to the param lists,
             * we don't want to run multiple times.
             */
          if (done)
            throw new IllegalStateException();
          done = true;

          //// reverse the order of our two lists
          // Collections.reverse(this.names);
          // Collections.reverse(this.values);

          // build a string from the parameter list
          StringBuffer newParams = new StringBuffer();
          for (int i = 0; i < names.size(); i++) {
            newParams.append(names.get(i) + "=" + values.get(i));
            if (i < (names.size() - 1))
                newParams.append("&");
          }

          // insert these parameters into the URL as appropriate
          if (newParams.length() > 0) {
              int questionMark = url.indexOf('?');
              if (questionMark == -1) {
                return (url + "?" + newParams);
              } else {
                StringBuffer workingUrl = new StringBuffer(url);
                workingUrl.insert(questionMark + 1, (newParams + "&"));
                return workingUrl.toString();
              }
          } else {
            return url;
          }
      }
    }
}
________________________________________________________________________________________________________________
ParamTag.java
_________________________________________

package dvdshop.tags;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
//import org.apache.taglibs.standard.tag.common.core.*;
//import org.apache.taglibs.standard.resources.Resources;

/**
 * <p>A handler for &lt;param&gt; that accepts attributes as Strings
 * and evaluates them as expressions at runtime.</p>
 *
 * @author Shawn Bayern
 */

public class ParamTag extends ParamSupport {

    //*********************************************************************
    // 'Private' state (implementation details)

    private String name_;                       // stores EL-based property
    private String value_;                  // stores EL-based property


    //*********************************************************************
    // Constructor

    /**
     * Constructs a new ParamTag.  As with TagSupport, subclasses
     * should not provide other constructors and are expected to call
     * the superclass constructor
     */
    public ParamTag() {
        super();
        init();
    }


    //*********************************************************************
    // Tag logic

    // evaluates expression and chains to parent
    public int doStartTag() throws JspException {

        // evaluate any expressions we were passed, once per invocation
 //       evaluateExpressions();

      // chain to the parent implementation
      return super.doStartTag();
    }


    // Releases any resources we may have (or inherit)
    public void release() {
        super.release();
        init();
    }


    //*********************************************************************
    // Accessor methods

    // for EL-based attribute
    public void setName(String name_) {
        this.name_ = name_;
    }

    public void setValue(String value_) {
        this.value_ = value_;
    }


    //*********************************************************************
    // Private (utility) methods

    // (re)initializes state (during release() or construction)
    private void init() {
        // null implies "no expression"
      name_ = value_ = null;
    }

    /* Evaluates expressions as necessary */
//    private void evaluateExpressions() throws JspException {
        /*
         * Note: we don't check for type mismatches here; we assume
         * the expression evaluator will return the expected type
         * (by virtue of knowledge we give it about what that type is).
         * A ClassCastException here is truly unexpected, so we let it
         * propagate up.
         */

//      name = (String) ExpressionUtil.evalNotNull(
//          "import", "name", name_, String.class, this, pageContext);
//      value = (String) ExpressionUtil.evalNotNull(
//          "import", "value", value_, String.class, this, pageContext);
    }

________________________________________________________________________________________________________________
UsePropertyTag.java
_________________________________________
package dvdshop.tags;

        //
        // Write your code here
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
 * This class is a custom action for making a bean property value
 * available to other actions and scripting code as a variable.
 * The property must be a multi-value property. The getter
 * method may take a String argument identifying the one value
 * to return.
 * @WebService
 * @tagName useProperty
 * @displayName <{UsePropertyTag}>
 * @TEIClass dvdshop.tags.UsePropertyTagExtraInfo
 * @tagBodyContent TAGDEPENDENT
 *
 */
public class UsePropertyTag extends TagSupport {
    private String id;
    private String name;
    private String property;
    private String arg;
    private String className;

    /**
     * Sets the id attribute, i.e. the name of the variable to hold the
     * reference to the selected property value.
     *
     * @param id the variable name
     */
    public void setId(String id) {
        this.id = id;
    }
   
    /**
     * Sets the name attribute, i.e. the name of the variable holding the
     * reference to the bean with the property to retrieve.
     *
     * @param name the bean name
     */
    public void setName(String name) {
        this.name = name;
    }
   
    /**
     * Sets the property attribute, i.e. the name of the property to
     * retrieve.
     *
     * @param property the property name
     */
    public void setProperty(String property) {
        this.property = property;
    }
   
    /**
     * Sets the arg attribute, i.e. the String argument value used
     * by the property getter method to select on of multiple property
     * values.
     *
     * @param arg the String argument value
     */
    public void setArg(String arg) {
        this.arg = arg;
    }

    /**
     * Sets the class attribute, i.e. the class name for the property
     * value.
     *
     * @param className the property class name
     */
    public void setClassName(String className) {
        this.className = className;
    }

    /**
     * Retrieves one value of a multi-valued property in the specified
     * bean using a getter method with a String argument. The value is
     * saved with the specified variable name in the page scope.
     */
    public int doEndTag() throws JspException {
        Object obj = pageContext.findAttribute(name);
        if (obj == null) {
            throw new JspException("Variable " + name + " not found");
        }
        Object propObj = getProperty(obj, property, className);
        pageContext.setAttribute(id, propObj);
          return SKIP_BODY;
    }
   
    /**
     * Releases all instance variables.
     */
    public void release() {
        id = null;
        name = null;
        property = null;
        arg = null;
        className = null;
        super.release();
    }
   
    /**
     * Returns the value of the specified property from the
     * specified bean.
     *
     * @param bean the bean
     * @param property the property name
     * @param propertyClassName the property class name (type)
     */
    private Object getProperty(Object bean, String property,
        String propClassName) throws JspException {
        Object propObj = null;
        Class beanClass = bean.getClass();
        Class[] params = null;
        if (arg != null) {
            // If an arg is specified, it must be a String arg
            params = new Class[1];
            params[0] = String.class;
        }
        Method method = null;
        try {
            /*
             * Since the method may have an arg, look for it explicitly
             * instead of using the standard property lookup method
             */
            method = beanClass.getMethod("get" +
                property.substring(0, 1).toUpperCase() + property.substring(1),
                params);
        }
        catch (NoSuchMethodException e) {
            throw new JspException("A property getter for " + property +
                (arg != null ? " with a String argument" : "") +
                " not found in " + name);
        }
        Class propClass = null;
        try {
          propClass = Class.forName(propClassName);
        }
        catch (ClassNotFoundException e) {
            throw new JspException("Property class " + propClassName + " not found");
        }

        Class returnType = method.getReturnType();
        if (!propClass.isAssignableFrom(returnType)) {
            throw new JspException("Property " + property + " is not a " + className);
        }
        Object[] args = null;
        if (arg != null) {
            args = new Object[1];
            args[0] = arg;
        }
        try {
            propObj = method.invoke(bean, args);
        }
        catch (Exception e) {
            throw new JspException("Failed to get property " + property + " from " + name);
        }
        return propObj;
    }
}
_________________________________________________________________________________________________________________
UsePropertyTagExtraInfo.java
________________________________________
/* Generated by Together */

package dvdshop.tags;

import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.VariableInfo;
import javax.servlet.jsp.tagext.TagData;

public class UsePropertyTagExtraInfo extends TagExtraInfo {
    public VariableInfo[] getVariableInfo(TagData data) {
        return new VariableInfo[]
        {
            new VariableInfo (data.getAttributeString("id"),
                data.getAttributeString("className"),
                true,
                VariableInfo.AT_END)
        };
    }
}
_______________________________________________________________________________________________________________
RedirectTag.java
_________________________________________

package dvdshop.tags;

import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
 * This class is a custom action for sending a redirect request,
 * with possible parameter values URL encoded and the complete URL
 * encoded for URL rewriting (session tracking).
 * @tagName redirect
 * @displayName <{RedirectTag}>
 * @persistent
 * @WebService
*/
public class RedirectTag extends TagSupport implements ParamParent {
    private String page;
    private Vector params;

    /**
     * Sets the page attribute.
     *
     * @param page the page URL to redirect to
     */
    public void setPage(String page) {
        this.page = page;
    }

    /**
     * Adds a parameter name and value. This method is called by param
     * tags in the action body.
     *
     * @param name the parameter name
     * @param value the URL encoded parameter value
     */
    public void addParameter(String name, String value) {
        if (params == null) {
            params = new Vector();
        }
        Param param = new Param(name, value);
        params.addElement(param);
    }

    /**
     * Override the default implementation so that possible
     * param actions in the body are processed.
     */
    public int doStartTag() {
        // Reset per-use state set by nested elements
      params = null;

        return EVAL_BODY_INCLUDE;
    }
   
    /**
     * Appends possible URL encoded parameters to the main URL,
     * encodes the result for URL rewriting. Clears the out buffer
     * and sets the redirect response headers. Returns SKIP_PAGE
     * to abort the processing of the rest of the page.
     */
    public int doEndTag() throws JspException {
        StringBuffer encodedURL = new StringBuffer(page);
        if (params != null && params.size() > 0) {
            encodedURL.append('?');
            boolean isFirst = true;
            Enumeration e = params.elements();
            while (e.hasMoreElements()) {
                Param p = (Param) e.nextElement();
                if (!isFirst) {
                    encodedURL.append('&');
                }
                encodedURL.append(p.getName()).append('=').append(p.getValue());
                isFirst = false;
            }
        }
        try {
            JspWriter out = pageContext.getOut();
            out.clear();
            HttpServletResponse res = (HttpServletResponse) pageContext.getResponse();
            res.sendRedirect(res.encodeURL(encodedURL.toString()));
        }
        catch (IOException e) {}
        return SKIP_PAGE;
    }
   
    /**
     * Releases all instance variables.
     */
    public void release() {
        page = null;
        params = null;
        super.release();
    }

    /**
     * This is a helper class that holds the name and value of a
     * parameter.
     */
    class Param {
        private String name;
        private String value;

        public Param(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public String getValue() {
            return value;
        }
    }
}
_____________________________________________________________________________________________________________________

DVDCartBean.java
________________________________
package dvdshop;

import java.io.*;
import java.util.*;

/**
 * This class represents a shopping cart. It holds a list of products.
 * @persistent
 *
 */
public class DVDCartBean implements Serializable {
    private Vector cart = new Vector();

    /**
     * Adds a product to the cart, if it's not already there.
     *
     * @param product the DVDProductBean
     */
    public void setProduct(DVDProductBean product) {
        if (product != null && cart.indexOf(product) == -1) {
            cart.addElement(product);
        }
    }

    /**
     * Returns the product list.
     *
     * @return an Enumeration of DVDProductBeans
     */
    public Enumeration getProducts() {
        return cart.elements();
    }

    /**
     * Returns the total price for all products in the cart
     *
     * @return the total price
     */
    public float getTotal() {
        float total = 0;
        Enumeration prods = getProducts();
        while (prods.hasMoreElements()) {
            DVDProductBean product = (DVDProductBean) prods.nextElement();
            float price = product.getPrice();
            total += price;
        }
        return total;
    }
   
    /**
     * Returns true if the cart is empty
     *
     * @return true if the cart is empty
     */
    public boolean isEmpty() {
        return cart.size() == 0;
    }
}


__________________________________________________________________________________________________________________
DVDCatalogBean.java
______________________________________
package dvdshop;

import java.io.*;
import java.util.*;

/**
 * This class represents a dvd product catalog. It holds a list of
 * products available for sale.
 * <p>
 * This is just a demo so the product list is hardcoded, created
 * at instantiation. A real version would get the information from
 * an external data source.
 * @persistent
 *
 */
public class DVDCatalogBean implements Serializable {

    /*
      public static void main(String[] args)
    {
        DVDCatalogBean db = new DVDCatalogBean();

        for(int i = 0; i < db.catalog.size(); i++)
            System.out.println(db.catalog.elementAt(i));
    }
      */

    private Vector catalog = new Vector();

    /**
     * Constructor. Creates all ProductBean objects and adds them
     * to the catalog.
     */
    public DVDCatalogBean() {
        DVDProductBean prod = new DVDProductBean();
        prod.setId("1");
        prod.setName("Robotech");
        prod.setDescr("One of my favorite animated series growing up where robots turn into jet planes and defends the Earth.");
        prod.setPrice(32.95f);
        catalog.addElement(prod);

        prod = new DVDProductBean();
        prod.setId("2");
        prod.setName("Transfomer");
        prod.setDescr("Ever wondered what those stupid robotic symbols on cars meant?  Well, you'll find the answer to this on my wall or here where robots battle each

other in disguise as automobiles and deceptive planes for the control of Earth.");
        prod.setPrice(32.95f);
        catalog.addElement(prod);

        prod = new DVDProductBean();
        prod.setId("3");
        prod.setName("Indiana Jones");
        prod.setDescr("A classic.  This unique trilogy stars Harrison Ford and is directed by Steven Spielberg and produced by George Lucas- 'nough said (somethig about

saving the planet Earth).");
        prod.setPrice(32.95f);
        catalog.addElement(prod);
    }

    /**
     * Returns a list of all products.
     *
     * @return a Enumeration with ProductBean elements
     */
    public Enumeration getProductList() {
      System.out.println("ProductList");
        return catalog.elements();
    }

    /**
     * Returns one product, or throws an exception if not found
     *
     * @param id the product id. The String version is to be able
     *   to use the useProperty action
     * @return a ProductBean
     * @exception Exception if the id doesn't match a product
     */
    public DVDProductBean getProduct(String id) throws Exception {
        boolean isFound = false;
        DVDProductBean product = null;
        Enumeration prods = getProductList();
        while (prods.hasMoreElements()) {
            product = (DVDProductBean) prods.nextElement();
            if (product.getId().equals(id)) {
                isFound = true;
                break;
            }
        }
        if (!isFound) {
            throw new Exception("Product id " + id + " doesn't match a product");
        }
        return product;
    }
}


_________________________________________________________________________________________________________________
DVDProductBean.java
________________________________________
package dvdshop;

import java.io.*;

  /**
  This class represents a product. It holds information about the
  product's name, description and price. All setter methods have
  package scope, since they are only used by the the CatalogBean.
  @persistent
  */

public class DVDProductBean implements Serializable {
    private String id;
    private String name;
    private String descr;
    private float price;

    /**
     * Returns the product id.
     *
     * @return the product id
     */
    public String getId() {
        return id;
    }

    /**
     * Returns the product name.
     *
     * @return the product name
     */
    public String getName() {
        return name;
    }

    /**
     * Returns the product description.
     *
     * @return the product description
     */
    public String getDescr() {
        return descr;
    }

    /**
     * Returns the product price.
     *
     * @return the product price
     */
    public float getPrice() {
        return price;
    }
   
    /**
     * Sets the product id.
     *
     * @param id the product id
     */
    void setId(String id) {
        this.id = id;
    }

    /**
     * Sets the product name.
     *
     * @param name the product name
     */
    void setName(String name) {
        this.name = name;
    }

    /**
     * Sets the product description.
     *
     * @param descr the product description
     */
    void setDescr(String descr) {
        this.descr = descr;
    }

    /**
     * Sets the product price.
     *
     * @param price the product price
     */
    void setPrice(float price) {
        this.price = price;
    }

      public String toString()
    {
        return "" + this.getId() + this.getName() + this.getPrice() + this.getDescr();
    }
}

________________________________________________________________________________________________________________________

All the other classes should not be a concern:
ELException.java
NullAttributeException.java
Resources.java
LoopTag1.java
LoppTag1ExtraInfo.java

But here it is anyway:
___________________________________________________________________________________________________________________
ELException.java
_________________________________________

package dvdshop.tags;


import java.text.MessageFormat;

/**
 *
 * Represents any of the exception conditions that arise during the
 * operation evaluation of the evaluator.
 *
 * @author Nathan Abramson - Art Technology Group
 * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: arista $
 **/

public class ELException
  extends Exception
{
  //-------------------------------------
  // Member variables
  //-------------------------------------

  Throwable mRootCause;

  //-------------------------------------
  /**
   *
   * Constructor
   **/
  public ELException ()
  {
    super ();
  }

  //-------------------------------------
  /**
   *
   * Constructor
   **/
  public ELException (String pMessage)
  {
    super (pMessage);
  }
  //-------------------------------------
  /**
   *
   * Constructor
   **/
  public ELException (Throwable pRootCause)
  {
    mRootCause = pRootCause;
  }

  //-------------------------------------
  /**
   *
   * Constructor
   **/
  public ELException (String pMessage,
                  Throwable pRootCause)
  {
    super (pMessage);
    mRootCause = pRootCause;
  }

  //-------------------------------------
  /**
   *
   * Returns the root cause
   **/
  public Throwable getRootCause ()
  {
    return mRootCause;
  }

  //-------------------------------------
  /**
   *
   * String representation
   **/
  public String toString ()
  {
    if (getMessage () == null) {
      return mRootCause.toString ();
    }
    else if (mRootCause == null) {
      return getMessage ();
    }

    else {
      return getMessage () + ": " + mRootCause;
    }
  }

  //-------------------------------------
}
_______________________________________________________________________________________________________________________
LoopTag1.java
__________________________________________
package dvdshop.tags;

import java.beans.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
 * This class is a custom action for looping through the elements
 * of a multi-valued bean or bean property. The bean or bean property
 * must be an array, a Vector, a Dictionary or an Enumeration. The
 * action body is evaluated once for each element.
 * @TEIClass dvdshop.tags.LoopTag1ExtraInfo
 * @tagName loop
 * @displayName <{LoopTag1}>
 *
 */
public class LoopTag1 extends BodyTagSupport {
    // Property variables
    private String name;
    private String property;
    private String loopId;
    private String className;
   
    // Enumeration used for iteration
    private Enumeration enum;

    /**
     * Sets the name attribute, i.e. the name of the variable holding
     * a reference to the bean with the multi-value property to loop
     * through.
     *
     * @param name the bean variable name
     */
    public void setName(String name) {
        this.name = name;
    }
   
    /**
     * Sets the property attribute, i.e. the name of the multi-value
     * property to loop through.
     *
     * @param property the property name
     */
    public void setProperty(String property) {
        this.property = property;
    }

    /**
     * Sets the loopId attribute, i.e. the name of the variable to
     * hold the element reference in the body.
     *
     * @param property the property name
     */
    public void setLoopId(String loopId) {
        this.loopId = loopId;
    }

    /**
     * Sets the class attribute, i.e. the name of the class for the
     * multi-value property elements.
     *
     * @param class the element class name
     */
    public void setClassName(String className) {
        this.className = className;
    }
   
    /**
     * Creates an Enumeration of all loop values, either using the object
     * specified by the name attribute directly, or using the specified
     * property. The object or the property must be one of Enumeration,
     * Vector, Dictionary or Object[] (no primitive type arrays), or a
     * subclass/subinterface of one of them.
     *
     * Makes the first element available to the body in a variable
     * with the name specified by the loopId attribute.
     */
    public int doStartTag() throws JspException {
        Object obj = pageContext.findAttribute(name);
        if (obj == null) {
            throw new JspException("Variable " + name + " not found");
        }
       
        Object mvObj = obj;
        try {
            // Get the multi-value object using the specified property getter
            // method, if any
            if (property != null) {
                mvObj = getProperty(obj, property);
            }

            enum = getEnumeration(mvObj);
        }
        catch (JspException e) {
            throw new JspException("Error getting loop data from " + name + ": " +
                e.getMessage());
        }
       
        // Set the first loop value, if any
        if (enum != null && enum.hasMoreElements()) {
            Object currValue = enum.nextElement();
            pageContext.setAttribute(loopId, currValue);
              return EVAL_BODY_TAG;
        }
        else {
              return SKIP_BODY;
        }
    }

    /**
     * Makes the next element available to the body in a variable
     * with the name specified by the loopId attribute, or returns
     * SKIP_BODY if all elements have been processed.
     */
    public int doAfterBody() throws JspException {
        if (enum.hasMoreElements()) {
            Object currValue = enum.nextElement();
            pageContext.setAttribute(loopId, currValue);
              return EVAL_BODY_TAG;
        }
        else {
              return SKIP_BODY;
        }
    }
   
    /**
     * Writes the accumulated body contents to the JspWriter.
     */
    public int doEndTag() throws JspException {
        // Test if bodyContent is set, since it will be null if the
        // body was never evaluated (doStartTag returned SKIP_BODY)
        if (getBodyContent() != null) {
            try {
                getPreviousOut().print(getBodyContent().getString());
            }
            catch (IOException e) {}
        }
        return EVAL_PAGE;
    }
   
    /**
     * Releases all instance variables.
     */
    public void release() {
        name = null;
        property = null;
        loopId = null;
        className = null;
        enum = null;
        super.release();
    }

    /**
     * Returns an Object representing an Enumeration, a Vector,
     * a Dictionary or an array of objects (no primitive types),
     * using the specified property getter method on the specified
     * object.
     *
     * @param obj the Object with the multi-value property
     * @param property the property name
     * @return the multi-value Object
     */
    private Object getProperty(Object obj, String property) throws JspException {
        Object mvObj = null;
       
        Method method = null;
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
            for (int i = 0; pds != null && i < pds.length; i++) {
                if (pds[i].getName().equals(property)) {
                    method = pds[i].getReadMethod();
                    break;
                }
            }
        }
        catch (IntrospectionException e) {
            throw new JspException("Error analyzing the bean class: " +
                e.getMessage());
        }

        if (method == null) {
            throw new JspException("Property " + property + " not found");
        }
       
        // Make sure the property is not of a primitive type
        if (method.getReturnType().isPrimitive()) {
            throw new JspException("Invalid property data type");
        }

        // Invoke the method to get the multi-value Object
        Object[] args = {};
        try {
            mvObj = method.invoke(obj, args);
        }
        catch (Exception e) {
            throw new JspException("Failed to get property " + property + ": " +
                e.getMessage());
        }
        return mvObj;
    }
   
    /**
     * Returns an Enumeration of the values in the specified multi-value
     * object, which can be an Enumeration, Vector, Dictionary or array
     * of Objects (not primitive types).
     *
     * @param obj the multi-value Object
     * @return an Enumeration
     * @exception JspException if invalid type
     */
    private Enumeration getEnumeration(Object obj) throws JspException {
        if (obj == null) {
            return null;
        }
       
        Enumeration enum = null;
        if (Enumeration.class.isAssignableFrom(obj.getClass())) {
            enum = (Enumeration) obj;
        }
        else if (Vector.class.isAssignableFrom(obj.getClass())) {
            enum = ((Vector) obj).elements();
        }
        else if (Dictionary.class.isAssignableFrom(obj.getClass())) {
            enum = ((Dictionary) obj).elements();
        }
        else if (obj.getClass().isArray()) {
            Object[] oa = (Object[]) obj;
            Vector v = new Vector(oa.length);
            for (int i = 0; i < oa.length; i++) {
                v.addElement(oa[i]);
            }
            enum = v.elements();
        }
        else {
            throw new JspException("Invalid data type");
        }
        return enum;
    }
}
_____________________________________________________________________________________________________________________
LoopTagExtraInfo.java
_________________________________________
/* Generated by Together */

package dvdshop.tags;

import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.VariableInfo;
import javax.servlet.jsp.tagext.TagData;

public class LoopTag1ExtraInfo extends TagExtraInfo {
    public VariableInfo[] getVariableInfo(TagData data) {
        return new VariableInfo[]
        {
            new VariableInfo(data.getAttributeString("loopId"),
                data.getAttributeString("className"),
                true,
                VariableInfo.NESTED)
        };
    }
}

_____________________________________________________________________________________________________________________
NullAttributeException.java
__________________________________________


package dvdshop.tags;

import javax.servlet.jsp.JspTagException;


/**
 * <p>NullAttributeException is a JspTagException that will be thrown
 * by the JSTL RI handlers when a tag attribute illegally evaluates
 * to 'null'.</p>
 *
 * @author Shawn Bayern
 */

public class NullAttributeException extends JspTagException {

    /**
     * Constructs a NullAttributeException with appropriate information.
     *
     * @param tag The name of the tag in which the error occurred.
     * @param att The attribute value for which the error occurred.
     */
    public NullAttributeException(String tag, String att) {
      super(Resources.getMessage("TAG_NULL_ATTRIBUTE", att, tag));
    }
}
_____________________________________________________________________________________________________________________
Resources.java
_________________________________________
package dvdshop.tags;

import java.util.*;

import java.text.*;

/**
 * <p>Provides locale-neutral access to string resources.  Only the
 * documentation and code are in English. :-)
 *
 * <p>The major goal, aside from globalization, is convenience.
 * Access to resources with no parameters is made in the form:</p>
 * <pre>
 *     Resources.getMessage(MESSAGE_NAME);
 * </pre>
 *
 * <p>Access to resources with one parameter works like</p>
 * <pre>
 *     Resources.getMessage(MESSAGE_NAME, arg1);
 * </pre>
 *
 * <p>... and so on.</p>
 *
 * @author Shawn Bayern
 */
public class Resources {

    //*********************************************************************
    // Static data

    /** The location of our resources. */
    private static final String RESOURCE_LOCATION
      = "org.apache.taglibs.standard.resources.Resources";

    /** Our class-wide ResourceBundle. */
    private static ResourceBundle rb =
      ResourceBundle.getBundle(RESOURCE_LOCATION);


    //*********************************************************************
    // Public static methods

    /** Retrieves a message with no arguments. */
    public static String getMessage(String name)
          throws MissingResourceException {
      return rb.getString(name);
    }

    /** Retrieves a message with arbitrarily many arguments. */
    public static String getMessage(String name, Object[] a)
          throws MissingResourceException {
      String res = rb.getString(name);
      return MessageFormat.format(res, a);
    }

    /** Retrieves a message with one argument. */
    public static String getMessage(String name, Object a1)
          throws MissingResourceException {
      return getMessage(name, new Object[] { a1 });
    }

    /** Retrieves a message with two arguments. */
    public static String getMessage(String name, Object a1, Object a2)
          throws MissingResourceException {
      return getMessage(name, new Object[] { a1, a2 });
    }

    /** Retrieves a message with three arguments. */
    public static String getMessage(String name,
                            Object a1,
                            Object a2,
                            Object a3)
          throws MissingResourceException {
      return getMessage(name, new Object[] { a1, a2, a3 });
    }

    /** Retrieves a message with four arguments. */
    public static String getMessage(String name,
                             Object a1,
                            Object a2,
                            Object a3,
                            Object a4)
          throws MissingResourceException {
      return getMessage(name, new Object[] { a1, a2, a3, a4 });
    }

    /** Retrieves a message with five arguments. */
    public static String getMessage(String name,
                            Object a1,
                            Object a2,
                            Object a3,
                            Object a4,
                            Object a5)
          throws MissingResourceException {
      return getMessage(name, new Object[] { a1, a2, a3, a4, a5 });
    }

    /** Retrieves a message with six arguments. */
    public static String getMessage(String name,
                            Object a1,
                            Object a2,
                            Object a3,
                            Object a4,
                            Object a5,
                            Object a6)
          throws MissingResourceException {
      return getMessage(name, new Object[] { a1, a2, a3, a4, a5, a6 });
    }

}
________________________________________________________________________________________








0
Comment
Question by:VWrestle97
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 16
  • 15
  • 10
  • +2
43 Comments
 
LVL 92

Expert Comment

by:objects
ID: 8096579
You do the following, but you the DVDProductBean  class does not have a getProduct() method.


   <dvd:useProperty id="product" name="catalog" property="product"
     arg="<%= request.getParameter(\"id\") %>"
     className="dvdshop.DVDProductBean" />
0
 
LVL 92

Expert Comment

by:objects
ID: 8096603
Actually that's not correct. It should be looking at the DVDCatalogBean class (I misread the code).

Can you change the message returned by the getProperty() method if your tag to return the bean class.

       catch (NoSuchMethodException e) {
           throw new JspException("A property getter for " + property +
               (arg != null ? " with a String argument" : "") +
               " not found in " + name + " of class "+beanClass);
       }

Though I think the problem is actually that the args aren't getting picked up from the tag. So it's looking for a getProduct() method that accepts no args, instead of one that accepts a String.

0
 
LVL 92

Expert Comment

by:objects
ID: 8096609
Could it be that 'id' is not in the request?

try this:

<%
   String id = request.getParameter("id");
   if (id==null)
   {
      id = "";
   }
%>

<dvd:useProperty id="product" name="catalog" property="product"
    arg="<%= id %>"
    className="dvdshop.DVDProductBean" />
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

Author Comment

by:VWrestle97
ID: 8097350

I am still getting the errors for the following jsp's
_____________________________________________________________________
DVDaddtocart.jsp
_________________________________________
Error: 500
Location: /jsp/DVDaddtocart.jsp
Internal Servlet Error:

org.apache.jasper.JasperException: Unable to compile class for JSPD:\ProgrammingLanguagefiles\Borland\TogetherControl6\myprojects\project\work\localhost_8080\_0002fjsp_0002fDVDaddtocart_0002ejspDVDaddtocart_jsp_0.java:203: Variable 'product' is already defined in this method.
                dvdshop.DVDProductBean product = null;
                                       ^
1 error
____________________________________________________________________
DVDproduct.jsp
__________________________________________
Error: 500
Location: /jsp/DVDproduct.jsp
Internal Servlet Error:

javax.servlet.ServletException: Failed to get property product from catalog

Root cause:
javax.servlet.jsp.JspException: Failed to get property product from catalog
     at dvdshop.tags.UsePropertyTag.getProperty(UsePropertyTag.java:165)
     at dvdshop.tags.UsePropertyTag.doEndTag(UsePropertyTag.java:94)

Warren

0
 
LVL 4

Expert Comment

by:boxy73
ID: 8098663
There is no "product" variable in your DVDProductBean.java, and there
are no getter o setter functions for them. If you're using:

<%-- Get the DVDProductBean from the catalog and save it in the cart --%>
 <dvd:useProperty id="product" name="catalog" property="product"
 arg='<%= request.getParameter("id") %>'
 className="dvdshop.DVDProductBean" />

you should have defined a "product" property, or you just wanted to
use another property and it is a mistake. I think you should use
property="id" instead.

0
 

Author Comment

by:VWrestle97
ID: 8098745
can you be more specific about the code that I am supposed to add to DVDProductBean.java....
also... are you telling me to do this?

<%-- Get the DVDProductBean from the catalog and save it in the cart --%>
<dvd:useProperty id="product" name="catalog" property="id"
arg='<%= request.getParameter("id") %>'
className="dvdshop.DVDProductBean" />
0
 

Author Comment

by:VWrestle97
ID: 8098894
boxy73,

isn't the getter function for product already in DVDCatalogBean.java
0
 
LVL 92

Expert Comment

by:objects
ID: 8099093
> Variable 'product' is already defined in this method

This is saying that product variable has already been defined.

> javax.servlet.ServletException: Failed to get property product from catalog

This is reporting that an exception occurred while calling the getter method.
Might be worthwhile including the details of the exception as part of the error message.
Possible reason for this exception is because the 'id' you passed was invalid.


0
 
LVL 92

Expert Comment

by:objects
ID: 8099107
Try this:

<%
  String id = request.getParameter("id");
  if (id==null)
  {
     id = "1";
  }
%>

<dvd:useProperty id="product" name="catalog" property="product"
   arg="<%= id %>"
   className="dvdshop.DVDProductBean" />
0
 

Author Comment

by:VWrestle97
ID: 8099235
I can't see where the product variable has already been deifined.... can you point it out?

0
 
LVL 92

Expert Comment

by:objects
ID: 8099286
Either could actually, let me have a closer look at your tags.

Regarding the other error, the problem appears that your add to cart link code below needs to include the product id as a param.

<a href="<dvd:encodeURL url="DVDaddtocart.jsp">

            </dvd:encodeURL>">Add this book to the shopping cart</a>

 
0
 
LVL 92

Expert Comment

by:objects
ID: 8099293
Something like:

<a href="<dvd:encodeURL url="DVDaddtocart.jsp">
  <dvd:param name="id" value="<%= product.getId() %>"/>
</dvd:encodeURL>">Add this book to the shopping cart</a>


0
 

Author Comment

by:VWrestle97
ID: 8099390
Error: 500
I still can't solve the following error.  I know what it is telling me but I can't find the duplicate

-----------------------------------------------------
Location: /jsp/DVDaddtocart.jsp
Internal Servlet Error:

org.apache.jasper.JasperException: Unable to compile class for JSPD:\ProgrammingLanguagefiles\Borland\TogetherControl6\myprojects\project\work\localhost_8080\_0002fjsp_0002fDVDaddtocart_0002ejspDVDaddtocart_jsp_0.java:203: Variable 'product' is already defined in this method.
                dvdshop.DVDProductBean product = null;
                                       ^
1 error

0
 
LVL 92

Expert Comment

by:objects
ID: 8099444
If you can email me the generated java I can see what the problem is.

JSPD:\ProgrammingLanguagefiles\Borland\TogetherControl6\myprojects\project\work\localhost_8080\_0002fjsp_0002fDVDaddtocart_0002ejspDVDaddtocart_jsp_0.java
0
 
LVL 92

Expert Comment

by:objects
ID: 8099819
addtocart should look something like:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html" %>
<%@ taglib uri="dvdtaglib.tld" prefix="dvd" %>
<%@ page import="java.text.*" %>
<%@ page import="dvdshop.DVDProductBean" %>
<%@ page import="dvdshop.DVDCatalogBean" %>
<%@ page import="dvdshop.DVDCartBean" %>

<html>

<HEAD>
  <META name="GENERATOR" content="IBM WebSphere Studio">
</HEAD>


<body>

 hi

  <jsp:useBean
  id="catalog"
  scope="application"
  class="dvdshop.DVDCatalogBean"
  />

  <jsp:useBean
  id="cart"
  scope="session"
  class="dvdshop.DVDCartBean"
  />

  <%-- Get the DVDProductBean from the catalog and save it in the cart --%>
  <dvd:useProperty id="product" name="catalog" property="product"
  arg='<%= request.getParameter("id") %>'
  className="dvdshop.DVDProductBean" />

  <jsp:setProperty name="cart" property="product" value="<%= product %>" />

  <%-- Redirect back to the catalog page --%>
  <dvd:redirect page="DVDcatalog.jsp" />
 
</body>
 
</html>

0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8100598
your original code works fine for me when I access it at

http://localhost:8080/DVDcatalog.jsp?id=1

after I fixed your dvdtaglib.tld as below, otherwise the context won't load:

   <info>
     Adds a parameter to a containing 'import' tag's URL.
   </info>

it seems that your exception handling code need some finetune. but the logic is fine.
0
 

Author Comment

by:VWrestle97
ID: 8100626
kennethxu,

I think that some code is missing from what you displayed for a comment line..... I thought that <info> lines do not get processed...

can you please further explain this...

if this is correct... where exactly do I add it?

warren
0
 
LVL 92

Expert Comment

by:objects
ID: 8100641
> otherwise the context won't load

If the context won't load then how is he getting errors using it :)
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8105817
>> can you please further explain this...
please find following segment in your dvdtaglib.tld file,
 <tag>
   <name>param</name>
   <tagclass>dvdshop.tags.ParamTag</tagclass>
   <bodycontent>JSP</bodycontent>
   <description>
     Adds a parameter to a containing 'import' tag's URL.
   </description>
   <attribute>
       <name>name</name>
       <required>true</required>
       <rtexprvalue>false</rtexprvalue>
   </attribute>
   <attribute>
       <name>value</name>
       <required>false</required>
       <rtexprvalue>false</rtexprvalue>
   </attribute>
 </tag>

according to taglib DTD 1.1, which you declared in the beginning of your jsp page, <tag> tag should NOT contain <description> tag. this problem prevented the taglib to be loaded by my server. I changed <description> tag to <info> tag and the application loads.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8105852
I would suggest you to remove <descrition> tag at all because it is just a informational tag.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8106381
OK, got some time to have a close look at your code. here is the changes that you need to make:

1. dvdtaglib.tld, change the value attribute of tag "param" to accept rtexprvalue:
 <tag>
   <name>param</name>
   <tagclass>dvdshop.tags.ParamTag</tagclass>
   <bodycontent>JSP</bodycontent>
   <info>
     Adds a parameter to a containing 'import' tag's URL.
   </info>
   <attribute>
       <name>name</name>
       <required>true</required>
       <rtexprvalue>false</rtexprvalue>
   </attribute>
   <attribute>
       <name>value</name>
       <required>false</required>
       <rtexprvalue>true</rtexprvalue>
   </attribute>
 </tag>

before:       <rtexprvalue>false</rtexprvalue>
after:       <rtexprvalue>true</rtexprvalue>

2. ParamTag.java, locate and change code.
before:
   public void setName(String name_) {
       this.name_ = name_;
   }

   public void setValue(String value_) {
       this.value_ = value_;
   }

after:
   public void setName(String name_) {
       name = this.name_ = name_;
   }

   public void setValue(String value_) {
       value = this.value_ = value_;
   }

3. DVDcatalog.jsp, remove a blank space after url link to DVDProduct.jsp
before:
          <dvd:encodeURL url="DVDproduct.jsp ">
              <dvd:param name="id" value="<%= product.getId() %>"/>
             </dvd:encodeURL>"><%= product.getName() %>
after:
          <dvd:encodeURL url="DVDproduct.jsp">
              <dvd:param name="id" value="<%= product.getId() %>"/>
             </dvd:encodeURL>"><%= product.getName() %>

4. DVDproduct.jsp, add id parameter to the url link to DVDaddtocart.jsp
before:
   <a href="<dvd:encodeURL url="DVDaddtocart.jsp">

            </dvd:encodeURL>">Add this book to the shopping cart</a>
after:
   <a href="<dvd:encodeURL url="DVDaddtocart.jsp">
              <dvd:param name="id"><%= request.getParameter("id") %></dvd:param>
            </dvd:encodeURL>">Add this book to the shopping cart</a>

recompile ParamTag.java, restart server and try again.

let me know if you have further enquires.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8106396
all above changes are based on your original code.
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 2000 total points
ID: 8106449
or you can download the fixed code here:
http://www.geocities.com/kennethxu/DVDshop.zip
0
 
LVL 92

Expert Comment

by:objects
ID: 8106749
kennethxu,

We've fixed those problems already.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8106856
objects, I see you have pointed out ONE of those problems, my item 4.
I cannot see the tld file and ParamTag.java been fixed anytime before, as well as my item 3?
it is important to fix the tld file and tag lib code, otherwise, nothing is going to pass down!
0
 
LVL 92

Expert Comment

by:objects
ID: 8106892
We've been working thru his code via email.
The ee interface sometimes can be a little inconvenient to interact with. Will post the completed code once Warren's happy.

My apologies for any inconvenience caused.
0
 

Author Comment

by:VWrestle97
ID: 8106894
kennethxu,

I don't think that the link ..
http://www.geocities.com/kennethxu/DVDshop.zip
is taking me to the correct URL
0
 

Author Comment

by:VWrestle97
ID: 8106926
kennethxu,

sorry... I got it
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8107003
>> We've been working thru his code via email.
I have just been told to EE mod that using email is considered as "abuse of pointing system", which against EE policy.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8107009
typo: I have just been told BY EE mod ...
0
 

Author Comment

by:VWrestle97
ID: 8107021
okay.. now I have got the id changing with each product link which is good... but I am still getting the same errors for DVDaddtocart.jsp and DVDproduct.jsp
0
 
LVL 92

Expert Comment

by:objects
ID: 8107054
> been told BY EE mod

i've been told that is it is ok (??), as long as the solution gets eventually posted.
Personally I rarely do it but sometimes it's just a lot easier when working thru code with a variety of errors.
0
 
LVL 92

Expert Comment

by:objects
ID: 8107177
> but I am still getting the same errors for DVDaddtocart.jsp and DVDproduct.jsp

are you sure the errors are the same.
That error implies that it is not recieving the id correctly.

Add some debug to display the product id in both those pages to confirm it is recieving the value.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8107480
>> i've been told that is it is ok (??), as long as the solution gets eventually posted.
this is a perfect example that it's unfair to other experts.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8107502
>> okay.. now I have got the id changing with each product link which is good... but I am still getting the same errors for DVDaddtocart.jsp and DVDproduct.jsp

do you refer to my code?

the whole app should start from DVDcatalog.jsp. DVDaddtocart.jsp and DVDproduct.jsp requests id parameter to be passed on for it to work properly. directly accessing to DVDaddtocart.jsp and DVDproduct.jsp is meaningless in this app model.
0
 
LVL 92

Expert Comment

by:objects
ID: 8107619
> this is a perfect example that it's unfair to other experts.

Yes but if all the discussion was posted here then this page would take forever to load.
Again my apologies for all this, but I was contacted via email and I replied via email and it just went from there.
Was just trying to help Warren get his project working.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8107930
>> if all the discussion was posted here then this page would take forever to load.
my fix does't seem that long.

>> Again my apologies for all this, but I was contacted via email and I replied via email and it just went from there.
OK, I hope you would post your fix along the way next time, so other experts won't waste their time, thank you.
0
 
LVL 92

Expert Comment

by:objects
ID: 8107995
> my fix does't seem that long.

It's not just a matter of a fix. It's about working thru the problem with the asker so they have an understanding of the problem and actually get to learn something.
I consider ee to not only about getting problems fixed, but also as a learning experience. Especially considering a large number of posters are students.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8112296
Surprised! thanks for your points VWrestle97.
Yes, my code works. But objects said, he had fixed all the problems that I fixed and gave a good lesson via email. If this is the case, you might consider to split the points.
I respect other experts time and effort. But of course, it's your judgement of the level of objects help.
thanks.
0
 

Author Comment

by:VWrestle97
ID: 8117045
thanks for your understanding kennethxu,

actually... if you don't mind, I would like to split it (2/3) or (50/50)... I just didn't want to make anyone feel cheated...let me know.

also if I wanted a customer to be able to delete an item from the shopping cart (for my program)... what would be the best code... can you help me out with this and show me where to put it.

thanks,
Warren
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8121284
>> actually... if you don't mind
I don't mind, and I think I have made it clear in my previous post.

>> also if I wanted a customer to be able to delete an item from the shopping cart (for my program)..

1. first you need to add a new method to your DVDCartBean, to perform the remove, for example, add this:

   public void setRemove(DVDProductBean product) {
        cart.remove(product);
   }

2. then you need to process the remove action in a jsp page, for example, copy DVDaddtocart.jsp to DVDremovefromcart.jsp, and modify the new jsp file as:
before:  <jsp:setProperty name="cart" property="product" value="<%= product %>" />
after:  <jsp:setProperty name="cart" property="remove" value="<%= product %>" />

3. at last, put a link on DVDcatalog.jsp to remove from cart, for example, replace the dvd:loop part to this:
          <dvd:loop name="cart" property="products" loopId="product" className="DVDProductBean">
            <tr>
              <td><%= product.getName() %></td>
              <td><%= numFormat.format(product.getPrice()) %></td>
              <td>
                     <a href="
                      <dvd:encodeURL url="DVDremovefromcart.jsp">
                           <dvd:param name="id" value="<%= product.getId() %>"/>
                          </dvd:encodeURL>">remove
                     </a>
                 </td>
            </tr>
          </dvd:loop>

          <tr><td colspan=3><hr></td></tr>
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8121326
remember you need to recompile DVDCartBean.java and restart server.
0
 

Expert Comment

by:modulo
ID: 8122839
Dear VWrestle97

I've refunded 200 points to enable you to post a
"Points for <expertname>" Q for the other expert in the same topic area.

Please:
1) Post the link to the original Q in the "Points for <expertname>" and
2) Add in the original Q a comment with the link to the "Points for <expertname>", thus the email notif will warn the expert.

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question