⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 复件 shoppingservlet.java

📁 jsp做的购物网站
💻 JAVA
字号:
package jspD;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShoppingServlet extends HttpServlet {
	public void init(ServletConfig conf) throws ServletException{
		super.init(conf);
	}
	public void doPost(HttpServletRequest req,HttpServletResponse res) 
		throws ServletException,IOException{
		HttpSession session=req.getSession(false);
			
		//redirect to error.html
		if(session==null){
			res.sendRedirect("Error.html");
		}
		Vector buylist=(Vector) session.getAttribute("shoppingcart");
		String action=req.getParameter("action");
		
		if(!action.equals("CHECKOUT")){
			
			//DELETE
			if(action.equals("DELETE")){
				String del=req.getParameter("del");
				int d=(new Integer(del)).intValue();	//Strin to int!!!!!!!
				buylist.removeElementAt(d);
			}
			else if(action.equals("ADD")){
				boolean match=false;
				
				//获取新增的商品
				Product aPd=getProduct(req);
				
				if(buylist==null){
					buylist=new Vector();
					buylist.addElement(aPd);
				}else{
					for(int i=0;i<buylist.size();i++){
						Product pdt=(Product)buylist.elementAt(i);
						if(pdt.getPName().equals(aPd.getPName())){
							pdt.setQuantity(pdt.getQuantity()+aPd.getQuantity());
							buylist.setElementAt(pdt,i);
							match=true;
						}
					}
					
					if(!match)
						buylist.addElement(aPd);
					
				}
			}
			
			session.setAttribute("shoppingcart",buylist);
			String url="cart/Eshop.jsp";
			ServletContext sc=getServletContext();
			RequestDispatcher rd=sc.getRequestDispatcher(url);
			rd.forward(req,res);
		}
		//结帐,计算总价钱
		else if(action.equals("CHECKOUT")){
			float total=0;
			for(int i=0;i<buylist.size();i++){
				Product order=(Product) buylist.elementAt(i);
				float price=order.getPrice();
				int quantity=order.getQuantity();
				total+=(price*quantity);
			}
			String amount=new Float(total).toString();	//float to Sting!!!
			req.setQttribute("amount",amount);
			String url="cart/Eshop.jsp";
			SetvletContext sc=getServletContext();
			RequestDispatcher rd=sc.getRequestDispatcher(url);
			rd.forward(req,res);
		}
	}//End of doPost
	
	private Product getProduct(HttpServletRequest req){
		String pName=encoding(req.getParameter("pName"));
		String quantity=encoding(req.getParameter("quantity"));
		String price=encoding(req.getParameter("price"));
		String producer=encoding(req.getParameter("producer"));
		
		Product pd=new Product();
		
		pd.setPName(pName);
		pd.setQuantity(quantity);
		pd.setPrice(price);
		pd.setProducer(producer);
		
		return pd;
	}		
			
}

⌨️ 快捷键说明

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