购物车程序源代码:
程序名为:catalogServlet代码如下:
/*
 * Created on 2009-4-25
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.servlet;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import  javax.servlet.http.HttpSession;
import  java.io.*;
import  java.util.*;
/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class CatalogServlet extends HttpServlet { protected void doGet(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//TODO Method stub generated by Lomboz
HttpSession  session=req.getSession();
int  itemCount=0;
HashMap  cart=(HashMap)session.getAttribute("cart");
if(cart!=null){
itemCount=cart.size();
}
res.setContentType("text/html;charset=UTF-8");
PrintWriter  out=res.getWriter();
out.println("<HTML><HEAD><TITLE>简单购物车 "
+"Example</TITLE></HEAD>");
out.println("<BODY><TABLE border=\"0\"  width=\"100%\"><tr>");
out.println("<td  align=\"left\" valign=\"bottom\">");
out.println("<H1>欢迎来到开心水果店购物</H1></td></tr></TABLE><HR>");
out.print("<FORM ACTION=\"");
out.println(res.encodeURL("cart"));
out.println("\" METHOD=\"POST\">");
out.println("<TABLE  CELLSPACING=\"5\"  CELLPADDING=\"5\"><TR>");
out.println("<TD  ALIGN=\"center\"><B>种类</B></TD>");
out.println("<TD  ALIGN=\"center\"><B>单价</B></TD>");
out.println("<TD  ALIGN=\"center\"><B>数量</B></TD></TR><TR>");
out.println("<TD  ALIGN=\"center\">"+"苹果"+"</TD>");
out.println("<TD  ALIGN=\"center\">"+"5.5"+"</TD>");
out.println("<TD  ALIGN=\"center\">");
out.println("<INPUT  NAME=\"apple_amount\""
+"></TD></TR><TR>");
out.println("<TD  ALIGN=\"center\">"+"香蕉"+"</TD>");
out.println("<TD  ALIGN=\"center\">"+"4.5"+"</TD>");
out.println("<TD  ALIGN=\"center\">");
out.println("<INPUT  NAME=\"banana_amount\""
+"></TD></TR><TR>");
out.println("<TD  ALIGN=\"center\">"+"葡萄"+"</TD>");
out.println("<TD  ALIGN=\"center\">"+"3.6"+"</TD>");
out.println("<TD  ALIGN=\"center\">");
out.println("<INPUT  NAME=\"grape_amount\""
+"></TD></TR>");
out.println("</TABLE><HR>");
out.println("<INPUT  TYPE=\"Submit\" NAME=\"btn_submit\""
+"VALUE=\"放入购物车\">");
out.println("</FORM></BODY></HTML>");
out.close();
}
}
运行没有问题,但到下一个cart就有问题了:源代码如下:
/*
 * Created on 2009-4-26
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.servlet;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import  javax.servlet.http.HttpSession;
import  java.io.*;
import  java.util.*;
/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class CartServlet extends HttpServlet { protected void doPost(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//TODO Method stub generated by Lomboz
HttpSession  session=req.getSession(true);
HashMap  cart=(HashMap)session.getAttribute("cart");
if(cart==null){
cart=new   HashMap();
cart.put("apple","0");
cart.put("banana","0");
cart.put("grape","0");
session.setAttribute("cart",cart);
}
res.setContentType("text/html;charset=UTF-8");
PrintWriter  out=res.getWriter();
req.setCharacterEncoding("UTF-8");
String  apple_amount=req.getParameter("applet_amount");
String  banana_amount=req.getParameter("banana_amount");
String  grape_amount=req.getParameter("grape_amount");
String  appleAmount=(String)cart.get("apple");
String  bananaAmount=(String)cart.get("banana");
String  grapeAmount=(String)cart.get("grape");
//int  te=Integer.parseInt(appleAmount,10);
        //int  te1=Integer.getInteger(apple_amount).intValue();
int  new_apple_amount=Integer.parseInt(appleAmount,10)+Integer.parseInt(apple_amount,10);
int  new_banana_amount=Integer.parseInt(bananaAmount,10)+Integer.parseInt(banana_amount,10);
int  new_grape_amount=Integer.parseInt(grapeAmount,10)+Integer.parseInt(grape_amount,10);
cart.put("apple",String.valueOf(new_apple_amount));
cart.put("banana",String.valueOf(new_banana_amount));
cart.put("grape",String.valueOf(new_grape_amount));
//Print  Current  Contents  of  Cart
out.println("<HTML><HEAD><TITLE>");
out.println("购物车内容");
out.println("</TITLE></HEAD>");
out.println("<BODY>");
out.println("<H1>你的购物车里有</H1>"+
new_apple_amount+"斤苹果"+new_banana_amount+"斤香蕉"
+new_grape_amount+"斤葡萄");
out.println("<HR>");
out.print("<HR><p><A  HREF=\"");
out.print(res.encodeURL("catalog"));
out.println("\">回到水果店</A></p>");
out.close();
}
}出现如下问题:
HTTP ERROR: 500 null
RequestURI=/SimpleServlet/cart;jsessionid=vl8ijr6yl9cq 
请高手解答一下。