gpt4 book ai didi

java - jsp:setProperty 请求 HttpServletRequest 处理 JSP 页面/Purchase.jsp 时发生异常

转载 作者:行者123 更新时间:2023-12-01 15:51:57 24 4
gpt4 key购买 nike

我有以下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>

<=- jsp 和 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()
{
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

最佳答案

JSP useBean 调用类的无参数构造函数。您只需在第二个构造函数(有一个参数:

)中初始化 cart
public ShoppingCart(String product)
{
cart = new Vector<String>();
}

这个构造函数永远不会被调用。

您需要在无参构造函数中初始化它,然后让第二个构造函数调用 this()

或者只是在声明中初始化cart:

private Vector<String> cart = new Vector<String>();

并将其保留在构造函数之外。

未初始化购物车会导致 addProductdeleteProduct 中出现 NullPointerException

关于java - jsp:setProperty 请求 HttpServletRequest 处理 JSP 页面/Purchase.jsp 时发生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5850833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com