我目前正在通过Sam的自学JSP工作。在一章中,我们编写了一个粗略的购物车应用程序,它使用Servlets来实现各种功能。在第14章中,我们使用bean替换大多数使用JSP的servlet。
出于某些原因,我的JSP无效。
/**
*
*/
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
}
}
和
<%-- 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上正确显示
由于它可能有助于理解出现了什么问题,因此这里还有两段代码:
<%@ 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>
<%@ 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>
<%
}
%>
我忽略了什么想法?
答案 0 :(得分:1)
我发现了问题。
以前,我使用一个名称作为持有ShoppingCart的变量“cart”。
我使用了第二个名称作为持有我的ShoppingCart,“ShoppingCart”的会话属性。
使用Beans,这些名称必须相同,因此我需要更改
<jsp:useBean id="cart" class="hu.flux.shoppingcart.ShoppingCart" scope="session" />
要
<jsp:useBean id="ShoppingCart" class="hu.flux.shoppingcart.ShoppingCart" scope="session" />
同样地:
<% cart.addItem(item); %>
要
<% ShoppingCart.addItem(item); %>
为了约定(用小写字母开始变量),最好改变对会话属性名称的所有其他引用,但这适用于“快速修复”。
答案 1 :(得分:0)
尝试以下方法(假设jsp位于根文件夹(webapp)中):
final RequestDispatcher rd = request.getRequestDispatcher("ShowShoppingCart.jsp");
rd.forward(request, response);
而不是重定向。我认为重定向会导致浏览器在获取对指定网址的响应时发出新请求。