jsp:setProperty request HttpServletRequest An exception occurred processing JSP page /Purchase.jsp
I have the following java and jsp. I get "org.apache.jasper.JasperException: An exception occurred processing JSP page /Purchase.jsp at line 41", that is the line with :"cart.processRequest(request);". Any idea please?
<!-- ShowSession.jsp -->
<%@page import="java.util.Vector"%>
<html>
<head>
<title>Shopping cart example</title>
</head>
<body bgcolor="#ffffcc">
<center>
<form type="post" action="Purchase.jsp">
<h1>Shopping cart example</h1>
</br>Please select a product and add it to your shopping cart</br>
<select name='product'>
<option>Beginning java 2 by Ivor Horton</option>
<option>Professional java programming by Brett Sell</option>
<option selected="selected">test</option>
<option>Professional jini by Sing Li</option>
<option>Professional JSP by Sing Li et all</option>
<option>Professional XLS by Andrew Watt et all</option>
<option>XML applications by frank Boumphrey et al</option>
<option>Beginning XML by Nikola Ozu et al</option>
<option>Instant UML by Pierra-Alain Muller</option>
<option>Beginning java objects by Jacquie Barker</option>
</select>
<input type="submit" name="submit" value="add">
</form>
<!-- Here goes the shopping cart display -->
<% String submit = request.getParameter("submit");
if(submit != null)
{
%>
<hr><h2 align="center">Your shopping cart</h2><p>
<jsp:useBean id="cart" scope="session" class="utilities.ShoppingCart" />
<jsp:setProperty name="cart" property="*" />
<% cart.processRequest(request); %>
<table width="75%" align="center" border="1">
<% Vector products = cart.getProducts();
for(int i=0; i<products.size(); i++)
{
%>
<tr bgcolor="#F9AD00">
<td><%=products.get(i) %></td>
<td><a href="Purchase.jsp?product=<%=products.get(i) %>&submit=remove">Remove</a></td>
</tr>
<% }
if(products.size() == 0)
{
%>
<tr>
<td>Your cart is currently empty</td>
</tr>
<% } %>
</table>
<% } %>
</center>
</body>
</html>
<=- jsp and java -=>
package utilities;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
public class ShoppingCart extends Object
{
private Vector<String> cart = null;
String product = null;
String submit = null;
public ShoppingCart() { }
public ShoppingCart(String product)
{
cart = new Vector<String>();
}
public void setProduct(String product)
{
this.product = product;
}
public void setSubmit(String submit)
{
this.submit = submit;
}
public Vector getProducts()
开发者_如何学C {
return cart;
}
public void addProduct(String product)
{
cart.add(product);
}
public void removeProduct(String product)
{
cart.remove(product);
}
public void processRequest(HttpServletRequest reg)
{
if(submit != null)
if(submit.equals("add"))
addProduct(product);
else
removeProduct(product);
reset();
}
public void reset()
{
submit = null;
product = null;
}
}
It gives me
org.apache.jasper.JasperException: An exception occurred processing JSP page /Purchase.jsp at line 41
JSP useBean calls the no-argument constructor of the class. You only initialize cart
in the second constructor (that has an argument:
public ShoppingCart(String product)
{
cart = new Vector<String>();
}
This constructor never gets called.
You need to initialize it in the no-argument constructor and then have the second constructor call this()
.
Or else just initialize cart
in the declaration:
private Vector<String> cart = new Vector<String>();
and leave it out of the constructor.
Leaving cart uninitialized causes a NullPointerException
in either addProduct
or deleteProduct
.
精彩评论