佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Jsp+Servlet實(shí)現(xiàn)購(gòu)物車功能

Jsp+Servlet實(shí)現(xiàn)購(gòu)物車功能

熱門標(biāo)簽:阿里云 科大訊飛語音識(shí)別系統(tǒng) 服務(wù)器配置 Linux服務(wù)器 電子圍欄 團(tuán)購(gòu)網(wǎng)站 銀行業(yè)務(wù) Mysql連接數(shù)設(shè)置

本文實(shí)例為大家分享了Servlet實(shí)現(xiàn)購(gòu)物車功能的具體代碼,供大家參考,具體內(nèi)容如下

(1)用servlet實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車系統(tǒng),項(xiàng)目結(jié)構(gòu)例如以下:(新建web Project項(xiàng)目  僅僅須要AddItemServlet , ListItemServlet。exam403.jsp三個(gè)文件就可以。其它的不用管)

(2)exam403.jsp代碼例如以下:

html xmlns="http://www.w3.org/1999/xhtml">
head>
meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
title>無標(biāo)題文檔/title>
/head>

body>
form id="form1" name="form1" method="post" action="/servletProject/addItem">
 label>/label>
 商品:
 select name="itemID" id="itemID">
 option value="洗衣粉">洗衣粉/option>
 option value="香皂">香皂/option>
 option value="食用油">食用油/option>
 /select>
 p>數(shù)量:
 label>
 input name="quantity" type="text" id="quantity" value="1" />
 /label>
 label>
 input type="submit" name="Submit" value="提交" />
 /label>
 a href="/servletProject/listItem">查看購(gòu)物車/a>/p>
/form>
/body>
/html>

(3)AddItemServlet代碼例如以下:

package com.lc.shoppingCar;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class AddItemServlet extends HttpServlet 
{ 
 protected void doGet(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,java.io.IOException
 {
 ServletContext application=getServletContext() ;
 ServletConfig config=getServletConfig() ;
 response.setContentType("text/html;charset=gb2312");
 PrintWriter out=response.getWriter(); 
 HttpSession session =request.getSession();
 request.setCharacterEncoding("gb2312");
 
 //讀取表單傳入的商品ID及數(shù)量
 String id=request.getParameter("itemID"); 
 String num=request.getParameter("quantity");
 if(id!=null  num.length()!=0)
 { //從Sessionn中讀取購(gòu)物車
 HashMap shoppingCar=(HashMap)session.getAttribute("shoppingCar");
 if(shoppingCar==null)
 shoppingCar=new HashMap();
 //將商品加入到購(gòu)物車中
 String onum=(String)shoppingCar.get(id);
 if(onum==null)
  shoppingCar.put(id,num);
 else
 {
 int n1=Integer.parseInt(num);
 int n2=Integer.parseInt(onum);
 String result=String.valueOf(n1+n2);
 shoppingCar.put(id,result);
 } 
  //將購(gòu)物車寫回session中保存
 session.setAttribute("shoppingCar",shoppingCar); 
 }
 else //假設(shè)傳入的商品ID號(hào)為空或數(shù)量為空。顯示提示信息
 System.out.print("商品ID號(hào)為空會(huì)或數(shù)量為空!");
 //返回商品列表頁(yè)
 response.sendRedirect("/servletProject/exam403.jsp"); 
 } 
 
protected void doPost(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,java.io.IOException
 {
 doGet(request,response);
 }
}

(4)ListItemServlet代碼例如以下:

package com.lc.shoppingCar;


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class ListItemServlet extends HttpServlet 
{ 
 protected void doGet(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,java.io.IOException
 {
 ServletContext application=getServletContext() ;
 ServletConfig config=getServletConfig() ;
 response.setContentType("text/html;charset=gb2312");
 PrintWriter out=response.getWriter(); 
 HttpSession session =request.getSession();
 request.setCharacterEncoding("gb2312");
 
 //從session中獲取購(gòu)物車
 HashMap shoppingCar=(HashMap)session.getAttribute("shoppingCar");
 //顯示購(gòu)物車中的內(nèi)容
 if(shoppingCar!=null)
 {
 Set show=shoppingCar.entrySet();
 Iterator it=show.iterator();
 while(it.hasNext())
 {
  out.print(it.next()+"br>");
 }
 }
 else
 out.print("購(gòu)物車為空。"); 
 } 
 
protected void doPost(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,java.io.IOException
 {
 doGet(request,response);
 }
}

(5)實(shí)現(xiàn)效果例如以下:

訪問:http://localhost:8080/servletProject/exam403.jsp    學(xué)則商品 提交

點(diǎn)擊查看購(gòu)物車:

OK!

簡(jiǎn)單的購(gòu)物車 到此結(jié)束!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • vuejs手把手教你寫一個(gè)完整的購(gòu)物車實(shí)例代碼
  • js購(gòu)物車實(shí)現(xiàn)思路及代碼(個(gè)人感覺不錯(cuò))
  • JavaScript編寫一個(gè)簡(jiǎn)易購(gòu)物車功能
  • Javascript實(shí)現(xiàn)購(gòu)物車功能的詳細(xì)代碼
  • js實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車有圖有代碼
  • Javascript操縱Cookie實(shí)現(xiàn)購(gòu)物車程序
  • 簡(jiǎn)單的前端js+ajax 購(gòu)物車框架(入門篇)
  • 原生js模擬淘寶購(gòu)物車項(xiàng)目實(shí)戰(zhàn)
  • js實(shí)現(xiàn)仿購(gòu)物車加減效果
  • js實(shí)現(xiàn)簡(jiǎn)易購(gòu)物車功能

標(biāo)簽:萍鄉(xiāng) 衢州 廣元 棗莊 江蘇 衡水 蚌埠 大理

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Jsp+Servlet實(shí)現(xiàn)購(gòu)物車功能》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    武冈市| 无棣县| 太湖县| 宁蒗| 汪清县| 五台县| 阜阳市| 社会| 和田县| 永泰县| 霍城县| 德保县| 晋中市| 双流县| 宜兰市| 南康市| 察隅县| 阿鲁科尔沁旗| 铜山县| 屯留县| 象州县| 思茅市| 仁布县| 青铜峡市| 饶河县| 静安区| 万安县| 安福县| 潞城市| 育儿| 乐都县| 克拉玛依市| 浦江县| 阿勒泰市| 南岸区| 于都县| 外汇| 长丰县| 绥阳县| 宿州市| 东明县|