title: Displaying Links to Products or Categories from the Controller Servlet into jsp pages
body: <p>I'm trying to link products to their own jsp page after a user chose a given product. I'm doing something wrong because the data doesn't display once on the product.jsp.</p>
  
  <p>I try to retrieve the data with the variable <code>selectedProduct</code> but it seems the application doesn't recognize it or doesn't know where to retrieve the data for the variable<code>selectedProduct</code>. </p>
  
  <p>I don't know why since, in my opinion, <code>selectedProduct</code> would be the same as <code>selectedCategory</code>... Apparently it is not... Why?  Am I missing something?</p>
  
  <p>I've been going through solutions for the past week. Any help is more than welcomed!</p>
  
  <p>Controller Servlet:</p>
  
  <pre><code>package controller;
  
  
  import cart.ShoppingCart;
  import wishlist.Wishlist;
  import entity.Category;
  import entity.Product;
  import java.io.IOException;
  import java.util.Collection;
  import javax.ejb.EJB;
  import javax.servlet.ServletConfig;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  import session.CategoryFacade;
  import session.ProductFacade;
  
  public class ControllerServlet extends HttpServlet {
  
  
      @EJB
      private CategoryFacade categoryFacade;
      @EJB
      private ProductFacade productFacade;
  
   @Override
      protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  
          String userPath = request.getServletPath();
          HttpSession session = request.getSession();
          Category selectedCategory;
          Product selectedProduct;
          Collection&lt;Product&gt; categoryProducts;
  
  
  
        // if category page is requested
              if (userPath.equals("/category")) {
  
                  // get categoryId from request
                  String categoryId = request.getQueryString();
  
                  if (categoryId != null) {
  
                      // get selected category
                      selectedCategory = categoryFacade.find(Short.parseShort(categoryId));
  
                      // place selected category in session scope
                      session.setAttribute("selectedCategory", selectedCategory);
  
                      // get all products for selected category
                      categoryProducts = selectedCategory.getProductCollection();
  
                      // place category products in session scope
                      session.setAttribute("categoryProducts", categoryProducts);
                  }
  
  
              // if product page is requested
              if (userPath.equals("/product")) {
  
                  // get productId from request
                  String productId = request.getQueryString();
  
                   if (productId != null) {
  
                  // get selected product
                  selectedProduct = productFacade.find(Short.parseShort(productId));
  
  // place selected product in session scope
                  session.setAttribute("selectedProduct", selectedProduct);
              }}
  </code></pre>
  
  <h1>EDIT BASED ON REQUEST</h1>
  
  <p><strong>Product.java:</strong></p>
  
  <pre><code>package entity;
  
  import java.io.Serializable;
  import java.math.BigDecimal;
  import java.util.Collection;
  import java.util.Date;
  import javax.persistence.Basic;
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.Lob;
  import javax.persistence.ManyToMany;
  import javax.persistence.NamedQueries;
  import javax.persistence.NamedQuery;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  import javax.persistence.Temporal;
  import javax.persistence.TemporalType;
  import javax.validation.constraints.NotNull;
  import javax.validation.constraints.Size;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.XmlTransient;
  
  /**
   *
   * @author PC
   */
  @Entity
  @Table(name = "product")
  @XmlRootElement
  @NamedQueries({
      @NamedQuery(name = "Product.findAll", query = "SELECT p FROM Product p"),
      @NamedQuery(name = "Product.findById", query = "SELECT p FROM Product p WHERE p.id = :id"),
      @NamedQuery(name = "Product.findByName", query = "SELECT p FROM Product p WHERE p.name = :name"),
      @NamedQuery(name = "Product.findByPrice", query = "SELECT p FROM Product p WHERE p.price = :price"),
      @NamedQuery(name = "Product.findByDescription", query = "SELECT p FROM Product p WHERE p.description = :description"),
      @NamedQuery(name = "Product.findByLastUpdate", query = "SELECT p FROM Product p WHERE p.lastUpdate = :lastUpdate")})
  public class Product implements Serializable {
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 45)
      @Column(name = "price")
      private BigDecimal price;
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "id")
      private Integer id;
      @Basic(optional = false)
      @NotNull
      @Lob
      @Size(min = 1, max = 16777215)
      @Column(name = "name")
      private String name;
      @Lob
      @Size(max = 2147483647)
      @Column(name = "description")
      private String description;
      @Basic(optional = false)
      @NotNull
      @Column(name = "last_update")
      @Temporal(TemporalType.TIMESTAMP)
      private Date lastUpdate;
      @JoinColumn(name = "category_id", referencedColumnName = "id")
      @ManyToMany(mappedBy = "productCollection")
      private Collection&lt;Category&gt; categoryCollection;
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "product")
      private Collection&lt;OrderedProduct&gt; orderedProductCollection;
  
  
      /**
       *
       */
      public Product() {
      }
  
      /**
       *
       * @param id
       */
      public Product(Integer id) {
          this.id = id;
      }
  
      /**
       *
       * @param id
       * @param name
       * @param price
       * @param lastUpdate
       */
      public Product(Integer id, String name, BigDecimal price, Date lastUpdate) {
          this.id = id;
          this.name = name;
          this.price = price;
          this.lastUpdate = lastUpdate;
      }
  
      /**
       *
       * @return
       */
      public Integer getId() {
          return id;
      }
  
      /**
       *
       * @param id
       */
      public void setId(Integer id) {
          this.id = id;
      }
  
      /**
       *
       * @return
       */
      public String getName() {
          return name;
      }
  
  
      /**
       *
       * @param name
       */
      public void setName(String name) {
          this.name = name;
      }
  
  
      /**
       *
       * @return
       */
      public String getDescription() {
          return description;
      }
  
      /**
       *
       * @param description
       */
      public void setDescription(String description) {
          this.description = description;
      }
  
      /**
       *
       * @return
       */
      public Date getLastUpdate() {
          return lastUpdate;
      }
  
      /**
       *
       * @param lastUpdate
       */
      public void setLastUpdate(Date lastUpdate) {
          this.lastUpdate = lastUpdate;
      }
  
      /**
       *
       * @return
       */
      @XmlTransient
      public Collection&lt;Category&gt; getCategoryCollection() {
          return categoryCollection;
      }
  
      /**
       *
       * @param categoryCollection
       */
      public void setCategoryCollection(Collection&lt;Category&gt; categoryCollection) {
          this.categoryCollection = categoryCollection;
      }
  
      /**
       *
       * @return
       */
      @XmlTransient
      public Collection&lt;OrderedProduct&gt; getOrderedProductCollection() {
          return orderedProductCollection;
      }
  
      /**
       *
       * @param orderedProductCollection
       */
      public void setOrderedProductCollection(Collection&lt;OrderedProduct&gt; orderedProductCollection) {
          this.orderedProductCollection = orderedProductCollection;
      }
  
      @Override
      public int hashCode() {
          int hash = 0;
          hash += (id != null ? id.hashCode() : 0);
          return hash;
      }
  
      @Override
      public boolean equals(Object object) {
          // TODO: Warning - this method won't work in the case the id fields are not set
          if (!(object instanceof Product)) {
              return false;
          }
          Product other = (Product) object;
          return !((this.id == null &amp;&amp; other.id != null) || (this.id != null &amp;&amp; !this.id.equals(other.id)));
      }
  
      @Override
      public String toString() {
          return "entity.Product[ id=" + id + " ]";
      }
  
      public BigDecimal getPrice() {
          return price;
      }
  
      public void setPrice(BigDecimal price) {
          this.price = price;
      }
  }
  </code></pre>
  
  <p>Product.jsp</p>
  
  <pre><code>&lt;table style="text-align: left; width: 100%; height: 172px;" border="0" cellpadding="0" cellspacing="0"&gt;
  
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td colspan="1" rowspan="6" style="vertical-align: top;"&gt;product_gallery&lt;br&gt;&lt;/td&gt;
              &lt;td colspan="1" rowspan="6" style="vertical-align: top;"&gt;&lt;img class="img" src="${initParam.productBigImagePath}${product.name}.jpg"&gt;&lt;br&gt;&lt;/td&gt;
              &lt;td style="vertical-align: top;"&gt;${selectedProduct.name}&lt;br&gt;&lt;/td&gt;
              &lt;td style="vertical-align: top;"&gt;&lt;br&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td style="vertical-align: top;"&gt;$ ${selectedProduct.price}&lt;/td&gt;
              &lt;td style="vertical-align: top;"&gt;&lt;br&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td style="vertical-align: top;"&gt;&lt;br&gt;&lt;/td&gt;
              &lt;td style="vertical-align: top;"&gt;&lt;br&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td style="vertical-align: top;"&gt;&lt;br&gt;&lt;/td&gt;
              &lt;td style="vertical-align: top;"&gt;&lt;br&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td colspan="2" rowspan="1" style="vertical-align: top;"&gt;${selectedProduct.description}&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td style="vertical-align: top;"&gt;
                  &lt;form action="addToWishlist" method="post"&gt;&lt;br&gt;&lt;br&gt; 
                      &lt;input name="productId" value="${product.id}" type="hidden"&gt; 
                      &lt;input class="submit" onclick="addedWishlist()" value="&lt;fmt:message key='AddToWishlist'/&gt;" type="submit"&gt; 
                  &lt;/form&gt;&lt;br&gt;
              &lt;/td&gt;
              &lt;td style="vertical-align: top;"&gt;
                  &lt;form action="addToCart" method="post"&gt;&lt;br&gt;&lt;br&gt; 
                      &lt;input name="productId" value="${product.id}" type="hidden"&gt;
                      &lt;input class="submit" onclick="addedCart()" value="&lt;fmt:message key='AddToCart'/&gt;" type="submit"&gt; 
                  &lt;/form&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td style="vertical-align: top;"&gt;&lt;br&gt;&lt;/td&gt;
              &lt;td style="vertical-align: top;"&gt;&lt;br&gt;&lt;/td&gt;
              &lt;td colspan="2" rowspan="1" style="vertical-align: top;"&gt;
                  &lt;ul&gt;
                      &lt;li style="background-color: rgb(198, 255, 201); width:100%; text-align:center; border-radius:2em;"&gt;
                          &lt;a href="${value}"&gt;&lt;fmt:message key='ContinueShopping'/&gt;&lt;/a&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;
                  &lt;br&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Category.jsp</p>
  
  <pre><code>&lt;table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="40px"&gt;
  
    &lt;c:forEach var="product" items="${categoryProducts}" varStatus="iter"&gt;
  
      &lt;td&gt;
        &lt;tbody&gt;
          &lt;tr&gt;
            &lt;td style="vertical-align: middle; text-align: center;" class="cell"&gt;
              &lt;a href="product?${product.id}"&gt;
  
  
  
                &lt;img class="img" alt="" src="${initParam.productImagePath}${product.name}.jpg" /&gt;
  
                &lt;div class="caption"&gt;
                  &lt;br&gt;view details&lt;/div&gt;
  
              &lt;/a&gt;
              &lt;br&gt;
  
  
            &lt;/td&gt;
            &lt;td style="vertical-align: middle; width: 140px; text-align: center; "&gt;${product.name}
              &lt;br&gt;
            &lt;/td&gt;
            &lt;td style="vertical-align: middle; width: 120px; text-align: center; line-height:100%;"&gt;$${product.price}
              &lt;br&gt;
            &lt;/td&gt;
            &lt;td style="vertical-align: middle; width: 136px; text-align: center; line-height:20%;"&gt;
  
              &lt;form id="wishlistForm" action="addToWishlist" method="post"&gt;
                &lt;br&gt;
                &lt;br&gt;
                &lt;input name="productId" value="${product.id}" type="hidden"&gt;
  
                &lt;input class="submit" onclick="addedWishlist()" value="&lt;fmt:message key='AddToWishlist'/&gt;" type="submit"&gt;
              &lt;/form&gt;
              &lt;br&gt;
            &lt;/td&gt;
            &lt;td style="vertical-align: middle; width: 136px; text-align: center; line-height:20%;"&gt;
  
  
              &lt;form id="cartForm" action="addToCart" method="post"&gt;
                &lt;br&gt;
                &lt;br&gt;
  
                &lt;input name="productId" value="${product.id}" type="hidden"&gt;
                &lt;input class="submit" onclick="addedCart()" value="&lt;fmt:message key='AddToCart'/&gt;" type="submit"&gt;
  
  
              &lt;/form&gt;
              &lt;br&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/c:forEach&gt;
  &lt;/table&gt;
  </code></pre>