ITPub博客

首页 > Linux操作系统 > Linux操作系统 > temp5

temp5

原创 Linux操作系统 作者:wgf19830218 时间:2011-05-07 15:46:18 0 删除 编辑

myCart.jsp

<%@ page language="java" import="java.util.*,com.wgf.*" pageEncoding="BIG5"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
ArrayList al = (ArrayList)request.getAttribute("ArrayList");
String goodsNum = (String)request.getAttribute("goodsNum");
out.print(goodsNum);
%>



 
   
   
    My JSP 'myCart.jsp' starting page
   
 
 
    
 
 
 
 
  function DeleteAllBooks()
  {
  form1.action = "MyCart?flag=clear";
  }
 
 
 
 
   


 
   
 
 
   
 
 
   
   
   
   
   
 
  <%
  float totalMoney=0;
  GoodsBean gb =null;
   for(int i=0;i   gb = (GoodsBean)al.get(i);
  %>
 

   
   
   
   
   
   
 
  <%
  totalMoney=totalMoney+gb.getGOODSPRICE();
  }
   %>
 
   
   
   
   
   
 
 
 

   
   
   
   
   
 
 
 
   
 
 
   
 
     
     
     
     
購 物 流 程 - - >我 的 購 物 車
     
     
     
     
編號
名稱
單价
數量
操作
<%=gb.getGOODSID() %>
<%=gb.getGOODSNAME() %>
<%=gb.getGOODSPRICE() %>

   
     
刪除
查看
 

     
   

     
   
 
 

     
       
         
         
       
     
你共選擇了價值<%=totalMoney %>元的商品,點此處繼續購物

           
         

   
     
     
     
     
     
     
     
     

 

 

 

 

 

 

 

 

 


控制器

myCart.java

 

package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import com.wgf.*;

public class MyCart extends HttpServlet {

 /**
  * The doGet method of the servlet.

  *
  * This method is called when a form. has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html");
  response.setCharacterEncoding("big5");
  
  String goodsname = request.getParameter("goodsname");
  //System.out.println("goodsname="+goodsname);
  
  MyCartDeal  mcd = (MyCartDeal)request.getSession().getAttribute("MyCart");
  
  if(mcd==null){
   mcd = new MyCartDeal();
   request.getSession().setAttribute("MyCart", mcd);
  }
  String clear = request.getParameter("flag");
  if(clear!=null){
  if(clear.equals("clear")){
   mcd.clearGoods();
   System.out.println("執行了");
  }
  }
  if(goodsname!=null){
  mcd.addGoods(goodsname,"1");
  }
  ArrayList al = mcd.getMyCartContent();
  request.setAttribute("ArrayList", al);
  String goodsNum = mcd.getNum(goodsname);
  System.out.println("goodsNum="+goodsNum);
  request.setAttribute("goodsNum", goodsNum);
  request.getRequestDispatcher("myCart.jsp").forward(request, response);
  
 }

 /**
  * The doPost method of the servlet.

  *
  * This method is called when a form. has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  this.doGet(request, response);
 }

}

 

 

 


MyCartDeal.java

package com.wgf;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

public class MyCartDeal {
 Connection con = null;
 Statement sm = null;
 ResultSet rs = null;
 
 HashMap hm = new HashMap();
 
 //添加
 public void addGoods(String goodsname,String num){
  hm.put(goodsname, num);
 }
 
 //刪除
 
 public void removeGoods(String goodsname){
  hm.remove(goodsname);
 }
 
 //清空
 
 public void clearGoods(){
  hm.clear();
 }
 
 //修改數量
 
 public void modifyGoodsNum(String goodsname,String num){
  hm.put(goodsname, num);
 }
 
 //獲取數量
 
 public String getNum(String goodsname){
  String goodsNum=null;
  goodsNum = hm.get(goodsname);
  return goodsNum;
 }
 
 
 
 //獲取購物車中的內容
 
 
 public ArrayList getMyCartContent(){
  ArrayList al = new ArrayList();
  con = new ConnectDB().getConnectDB();
  String nameContent="";
  if(hm.size()>0){
  Iterator it = hm.keySet().iterator();
  String sql = "select  *  from ptc.goods where goodsname in(";
  while(it.hasNext()){
   sql = sql+it.next();
   if(it.hasNext()){
    sql=sql+",";
   }
   else{
    sql=sql+")";
   }
  }
  
  sql = sql+nameContent;
  System.out.println(sql);
  try {
   sm = con.createStatement();
   rs = sm.executeQuery(sql);
   while(rs.next()){
    GoodsBean gb = new GoodsBean();
    gb.setGOODSID(rs.getInt(1));
    gb.setGOODSNAME(rs.getString(2));
    gb.setGOODSINTRO(rs.getString(3));
    gb.setGOODSPRICE(rs.getFloat(4));
    gb.setGOODSNUM(rs.getInt(5));
    gb.setGOODSPUBLISHER(rs.getString(6));
    gb.setPHOTONAME(rs.getString(7));
    gb.setGOODSTYPE(rs.getString(8));
    al.add(gb);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  finally{
   this.closeResource();
  }
  }
  return al;
 }
 
 public void closeResource() {
  try {
   if (rs != null) {
    rs.close();
   }
   if (sm != null) {
    sm.close();
   }
   if (con != null) {
    con.close();
   }
  } catch (Exception e) {
   e.getStackTrace();
  }
 }
 
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22453766/viewspace-694741/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: temp4
下一篇: temp7
请登录后发表评论 登录
全部评论

注册时间:2009-09-01

  • 博文量
    38
  • 访问量
    52773