我有以下java和jsp。我得到“org.apache.jasper.JasperException:在第41行处理JSP页面/Purchase.jsp时发生异常”,即:“cart.processRequest(request);”。好吗?
<!-- 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>
&lt; = - jsp和java - =&gt;
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()
{
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;
}
}
它给了我
org.apache.jasper.JasperException: An exception occurred processing JSP page /Purchase.jsp at line 41
答案 0 :(得分:1)
JSP useBean调用该类的无参数构造函数。您只在第二个构造函数中初始化cart
(具有参数:
public ShoppingCart(String product)
{
cart = new Vector<String>();
}
这个构造函数永远不会被调用。
您需要在无参数构造函数中初始化它,然后让第二个构造函数调用this()
。
或者只是在声明中初始化cart
:
private Vector<String> cart = new Vector<String>();
并将其从构造函数中删除。
让购物车未初始化会导致NullPointerException
或addProduct
中的deleteProduct
。