下载源程序文件
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ShoppingCart extends HttpServlet {
public void doPost(HttpServletRequest req,HttpServletResponse res) throws
ServletException ,IOException{
//取得Session对象
//假如Session不存在,为本次会话创建此对象
HttpSession session =req.getSession(true);
Integer itemCount=(Integer)session.getValue("itemCount");
//假如session是新的
if (itemCount==null)
itemCount=new Integer(0);
PRintWriter out=res.getWriter();
res.setContentType("text/html");
//接收传来的参数
String[] itemsSelected;
String itemName;
itemsSelected=req.getParameterValues("item");
if(itemsSelected !=null){
for(int i=0;i itemName=itemsSelected[i];
System.out.println(itemName);
itemCount=new Integer(itemCount.intValue()+1);
//购买的条目
session.putValue("item"+itemCount,itemName);
//总条目
session.putValue("itemCount",itemCount);
}
}
out.println("<html>");
out.println("<title>");
out.println("item list");
out.println("</title>");
out.println("<body><hr>Session List:</h4><br><br>");
for(int i=1;i<=itemCount.intValue();i++){
out.println((String)session.getValue("item"+i)+"<hr>");
}
out.println("</body>");
out.println("</html>");
out.close();
}
}
新闻热点
疑难解答