I am using Eclipse Helios with Struts and am having what I imagine is a rookie problem: for the life of me I have not been able to figure out why my jsp page will not acknowledge my business object .java file.
I think it has something to do with the .java class files as I have been unable to get them to appear in the IMPORTED CLASSES section under my Library Resources heading. The best I have been able to do thus far is load the class files as a seperate jar (which of course still doesn’t appear in the IMPORTED CLASSES section).
Does anyone know why my cart.jsp file refuses to allow me to reference my Cart.java business object? I get a null pointer error when I try to create the Cart, LineItem, and Product objects below.
Here are some snippets:
webapp\WebContent\example\cart.jsp
<%@ page import="bo.*, java.util.ArrayList" %>
<%
Cart cart = (Cart) session.getAttribute("cart");
ArrayList<LineItem> items = cart.getItems();
for (LineItem item : items)
{
Product product = item.getProduct();
%>
<tr valign="top">
<td>
<form action="<%=response.encodeURL("cart")%>" method="post">
<input type="hidden" name="productCode"
value="<%=product.getCode()%>">
<input type=text size=2 name="quantity"
value="<%=item.getQuantity()%>">
<input type="submit" value="Update">
</form>
</td>
<td>
<%=product.getDescription()%>
</td>
<td>
<%=product.getPriceCurrencyFormat()%>
</td>
<td>
<%=item.getTotalCurrencyFormat()%>
</td>
<td>
<form action="<%= response.encodeURL("cart")%>" method="post">
<input type="hidden" name="productCode"
value="<%=product.getCode()%>">
<input type="hidden" name="quantity"
value="0">
<input type="submit" value="Remove Item">
</form>
</td>
</tr><% } %>
Java Resources\src\bo\Cart.java
package bo;
import java.util.*;
import java.io.Serializable;
public class Cart implements Serializable
{
private ArrayList<LineItem> items;
public Cart()
{
items = new ArrayList<LineItem>();
}
public void setItems(ArrayList<LineItem> lineItems)
{
items = lineItems;
}
etc. . . .
Java Resources\src\bo\DisplayCartServlet.java
package action;
import java.io.*;
import java.sql.SQLException;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import bo.*;
import dao.*;
public class DisplayCartServlet extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
String forward = new String("success"); ;
String productCode = request.getParameter("productCode");
HttpSession session = request.getSession();
Cart cart = (Cart) session.getAttribute("cart");
if (cart == null)
{
cart = new Cart();
session.setAttribute("cart", cart);
}
int quantity = 1;
// Get product from product code
Product product=null;
try {
product = ProductDB.selectProduct(productCode);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.setAttribute("product", product);
// If product exists, add or remove from cart
if (product != null)
{
LineItem lineItem = new LineItem();
lineItem.setProduct(product);
lineItem.setQuantity(quantity);
if (quantity > 0)
cart.addItem(lineItem);
else
cart.removeItem(lineItem);
}
session.setAttribute("cart", cart);
return(mapping.findForward(forward));
}
}
Struts.xml declaration:
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="example" namespace="/example" extends="struts-default">
<action name="cart" class="action.DisplayCartServlet" method="execute">
<result name="success">/example/cart.jsp</result>
</action>
</package>
. . . .
</struts>
Link that requests action/servlet:
<div id="cartLink"><a href="<s:url action="cart?productCode=XM123456"/>">Add to Cart</a></div>
You expect a cart to be present in a (new) HttpSession. Given an HttpSession exists because you are running in tomcat/jetty and either of these servlet containers will create a session for you.
Now you ask for a cart in that session. Why do you expect it to be there?
You could create one if session.getAttribute(“cart”) returns null and store it in your session object.