Jsp+Servlet實現(xiàn)購物車功能
本文實例為大家分享了Servlet實現(xiàn)購物車功能的具體代碼,供大家參考,具體內(nèi)容如下
(1)用servlet實現(xiàn)簡單的購物車系統(tǒng),項目結(jié)構(gòu)例如以下:(新建web Project項目 僅僅須要AddItemServlet , ListItemServlet。exam403.jsp三個文件就可以。其它的不用管)
(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">查看購物車</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中讀取購物車 HashMap shoppingCar=(HashMap)session.getAttribute("shoppingCar"); if(shoppingCar==null) shoppingCar=new HashMap(); //將商品加入到購物車中 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); } //將購物車寫回session中保存 session.setAttribute("shoppingCar",shoppingCar); } else //假設(shè)傳入的商品ID號為空或數(shù)量為空。顯示提示信息 System.out.print("商品ID號為空會或數(shù)量為空!"); //返回商品列表頁 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中獲取購物車 HashMap shoppingCar=(HashMap)session.getAttribute("shoppingCar"); //顯示購物車中的內(nèi)容 if(shoppingCar!=null) { Set show=shoppingCar.entrySet(); Iterator it=show.iterator(); while(it.hasNext()) { out.print(it.next()+"<br>"); } } else out.print("購物車為空。"); } protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException { doGet(request,response); } }
(5)實現(xiàn)效果例如以下:
訪問:http://localhost:8080/servletProject/exam403.jsp 學(xué)則商品 提交
點擊查看購物車:
OK!
簡單的購物車 到此結(jié)束!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
CORBA對象生命周期之實現(xiàn)和內(nèi)存管理
CORBA對象生命周期之實現(xiàn)和內(nèi)存管理...2006-10-10JSP頁面中如何用select標(biāo)簽實現(xiàn)級聯(lián)
在JSP頁面中如何用select標(biāo)簽實現(xiàn)級聯(lián)呢?以下小編就為大家介紹實現(xiàn)方法,需要的朋友可以參考下2013-07-07Jsp結(jié)合XML+XSLT將輸出轉(zhuǎn)換為Html格式
Jsp結(jié)合XML+XSLT將輸出轉(zhuǎn)換為Html格式...2006-10-10關(guān)于JSP用戶登錄連接數(shù)據(jù)庫詳情
這篇文章主要介紹了關(guān)于JSP用戶登錄連接數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容2021-09-09