【发布时间】:2010-10-18 20:50:44
【问题描述】:
我目前正在通过 Sam's Teach Yourself JSP 工作。在一章中,我们编写了一个粗略的购物车应用程序,它使用 Servlet 实现各种功能。在第 14 章中,我们使用 bean 将大部分 servlet 替换为 JSP。
由于某种我无法理解的原因,我的 JSP 无法正常工作。
这是函数式servlet:
/**
*
*/
package hu.flux.shoppingcart;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* @author Brian Kessler
*
*/
public class AddToShoppingCartServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@SuppressWarnings("deprecation")
public void service (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
// First get the item values from the request.
String productCode = request.getParameter("productCode");
String description = request.getParameter("description");
int quantity = Integer.parseInt(request.getParameter("quantity"));
double price = Double.parseDouble(request.getParameter("price"));
// Now create an item to add to the cart.
Item item = new Item(productCode, description, price, quantity);
HttpSession session = request.getSession();
ShoppingCart cart = ShoppingCart.getCart(session);
cart.addItem(item);
// Now display the cart and allow the user to check out or order more items.
response.sendRedirect(
response.encodeRedirectUrl(ShoppingCart.SHOPPING_CART_PATH + "/ShowShoppingCart.jsp"));
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
和
这是功能失调的 JSP:
<%-- Get a reference to the shopping cart --%>
<jsp:useBean id="cart" class="hu.flux.shoppingcart.ShoppingCart" scope="session" />
<%-- Create an item object --%>
<jsp:useBean id="item" class="hu.flux.shoppingcart.Item" scope="page" />
<%-- Copy the request parameters into the item --%>
<jsp:setProperty name="item" property="*"/>
<%-- Add the item to the shopping cart --%>
<% cart.addItem(item); %>
<%--Display the product catalog again --%>
<jsp:forward page="ShowShoppingCart.jsp" />
为了让事情变得神秘,没有显示任何错误。最后一行肯定可以正常工作,因为它成功转发到 ShowShoppingCart.jsp,但是购物车是空的。
经过实验,我得出的结论是,无论发生什么问题,都必须在调用时或之后发生。我知道这一点是因为我将 ShowShoppingCart.jsp 上的一些代码移到了 AddToShoppingCart.jsp 上,并且(经过一些调试),购物车将在 AddToShoppingCart.jsp 上正确显示
因为它可能有助于理解出了什么问题,这里还有两段代码:
*这里是jsp调用的页面:forward page="ShowShoppingCart.jsp" *
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="hu.flux.shoppingcart.*"%>
<%
String shoppingCartSerialization = "";
ShoppingCart cart = ShoppingCart.getCart(session);
shoppingCartSerialization = cart.getSerialization();
Cookie cookie = new Cookie ("ShoppingCart", shoppingCartSerialization);
cookie.setMaxAge(999999999);
response.addCookie (cookie);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#ffffff">
<div><jsp:include page="DisplayShoppingCart.jsp" flush="true" /></div>
<form action="<%=ShoppingCart.SHOPPING_CART_PATH %>/Checkout.jsp"
method="post" >
You may <a
href="<%=ShoppingCart.SHOPPING_CART_PATH %>/ShowProductCatalog2.jsp"
>continue shopping</a> or
<input type="submit" value="Check Out">
</form>
<br/><br/><br/><hr/>
<form action="TerminateShoppingSessionServlet" method="post"><input
type="submit" value="Terminate Session"></form>
</body>
</html>
*这里是jsp调用的页面:include page="DisplayShoppingCart.jsp" flush="true" *
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="hu.flux.shoppingcart.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<%-- Show the header with the shopping cart item --%>
<img id="cart_image"
src="/SamsTeachYourselfJSP/ShoppingCart/images/cart4.png" />
<h1>Shopping Cart</h1>
<% // Get the current shopping cart from the user's session.
ShoppingCart cart = ShoppingCart.getCart(session);
// Get the items from the cart.
Vector<Item> items = cart.getItems();
// If there are no items, tell the user that the cart is empty.
if (items.size() == 0) {%><strong>Your shopping cart is empty.</strong>
<%}
else
{
%>
<%-- Display the header for the shopping cart table --%>
<br />
<table border="4">
<tr>
<th>Description</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<%
int numItems = items.size();
// Get a formatter to write out currency values.
NumberFormat currency = NumberFormat.getCurrencyInstance();
for (int i=0; i<numItems; i++)
{
Item item = (Item)items.elementAt(i);
out.print (item.printShoppingCartTableRow(currency,
// "/SamsTeachYourselfJSP/ShoppingCart/RemoveItemServlet?item="+i)
"/SamsTeachYourselfJSP/ShoppingCart/RemoveItem.jsp?item="+i)
);
}
%>
</table>
<%
}
%>
任何想法我忽略了什么?
【问题讨论】:
-
也许错误信息会有所帮助
-
抱歉,没有错误信息。系统一直在运行,只是在使用 JSP 时不会将任何商品添加到购物车中。
标签: java jsp session servlets javabeans