clientshopaction.java

来自「电子商务网站使用MVC模式B/S结构功能不是很全适合初学者看」· Java 代码 · 共 84 行

JAVA
84
字号
package Client;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import Common.DBConnectBean;

public class clientShopAction extends HttpServlet{
	public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		ServletContext application=getServletConfig().getServletContext(); 
		request.setCharacterEncoding("gb2312");
		HttpSession session=request.getSession();
		String username=(String)session.getAttribute("username".toUpperCase());
		////////////////////System.out.println("这个没打印");
		if(username==null){
			application.getRequestDispatcher("/client/clientList.jsp?logged=false").forward(request,response);	
		}
		else{
			ArrayList commodityList=new ArrayList();
			ArrayList commodityListCopy=new ArrayList();
			Hashtable commodity=new Hashtable();
			String[] commodityID=request.getParameterValues("id");
			if (commodityID==null) System.out.print("   wakao zmshehihih");
			if(commodityID!=null){
				DBConnectBean dbBean=new DBConnectBean("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:shopping","","");
				
				if(session.getAttribute("commodityList".toUpperCase())!=null){
					//获取现在客户的购物车里的所有商品的信息及数量
					commodityList=commodityListCopy=(ArrayList)session.getAttribute("commodityList".toUpperCase());
				}
				for (int i=0;i<commodityID.length;i++){
					int j=0;
					System.out.println("the client now has buy ");
					//如果客户已经买了该商品,则添加商品数量
					if(commodityListCopy!=null){
					//	System.out.println("不空不空");
						for(;j<commodityListCopy.size();j++){
							Hashtable tb=(Hashtable)commodityListCopy.get(j);
							String id=tb.get("id".toUpperCase()).toString();
						//	System.out.print("------finding   "+id+commodityID[i]);
							if(id.equals(commodityID[i])){
								System.out.println("found  ");
								commodity=tb;
								commodityList.remove(j);
								int commodityNum=Integer.parseInt(tb.get("commodityNum".toUpperCase()).toString())+1;
								commodity.put("commodityNum".toUpperCase(),commodityNum);
								commodityList.add(j,commodity);
							//	System.out.println("find one has bought");	
								break;
							}
						}
					}else {
						System.out.println("这里是空的");
					}
					//如果可户还未买该商品,则添加新商品
					if(commodityListCopy==null||j==commodityListCopy.size()){
						Hashtable tmptable=new Hashtable();
						System.out.println("put this commodity   "+commodityID[i]);
						tmptable=dbBean.getOneRecord("select card,num,price,picture from commodity where id="+commodityID[i]);
						commodity=new Hashtable();
						commodity.put("id".toUpperCase(),commodityID[i]);
						commodity.put("card".toUpperCase(),tmptable.get("card".toUpperCase()));
						commodity.put("num".toUpperCase(),tmptable.get("num".toUpperCase()));
						commodity.put("price".toUpperCase(),tmptable.get("price".toUpperCase()));
						commodity.put("picture".toUpperCase(),tmptable.get("picture".toUpperCase()));
						commodity.put("commodityNum".toUpperCase(),1);
						commodityList.add(commodity);	
					}
				}
			//	System.out.println("武打无扎");
				session.setAttribute("commodityList".toUpperCase(),commodityList);
			}
		}
	
		application.getRequestDispatcher("/client/clientList.jsp?done=true").forward(request,response);
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
	   	doGet(request,response);
	}
}
	
	
	

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?