代码如下:
<html>
<head>
<title>
carts
</title>
</head>
<body bgcolor="white">
<form action="carts.jsp" meteod="post">
<font size="4" color="#CC0000">
Please enter item to add or remove
<br>
Add item:
<select name="item">
<option>A
<option>B
<option>C
</select>
<p><input type="submit" value="add">
<input type="submit" value="remove"></P>
</font>
</form>
</body>
</html>

解决方案 »

  1.   

    package com.mycompany.bean;import java.util.Vector;
    import javax.servlet.http.*;
    import java.util.Enumeration;public class DummyCarts {
      Vector v= new Vector();
      String submit=null;
      String item=null;  private void addItem(String name){
        if (name != null)
      v.addElement(name);}private void removeItem(String name){
        v.remove(name);}public void setItem(String name){
       item=name;} public void setSubmit(String s){
       submit=s;}
     public String[]getItems(){
       String[]s=new String[v.size()];
       v.copyInto(s);
       return s;
     } public void processRequest(HttpServletRequest request){
       if(submit==null)
           addItem(item);
           else if(submit.equals("add"))
             addItem(item);
           else if(submit.equals("remove"))
             removeItem(item);
             reset();
     } private void reset(){
       submit = null;
     item=null;
     }
     }
      

  2.   

    <%@ page contentType="text/html; charset=GBK" %>
    <html>
    <head>
    <title>
    carts
    </title>
    </head>
    <jsp:useBean id="cart" scope="session" class="com.mycompany.bean.DummyCarts" />
    <jsp:setProperty name="cart" property="*" />
    <%
    cart.processRequest(request);
    %>
    <font size=4 color="#CC000">
    <br>You have the following items in your carts;
    </font>
    <o1>
    <%
    String[]items=cart.getItems();
    for( int i=0; i<items.length;i++){
    %>
    <li>
    <%=items[i]%>
    <%}%>
    </o1>
    <hr>
    <%@ include file="carts.html"%>
    </html>
      

  3.   

    就是点击remove的时候还是添加商品,没有删除商品。
      

  4.   

    servlet 中的String submit;又没给它赋值,processRequest()你传个request对象又没用,不还是=null,所有都是add