Summary: Create a simple shopping cart with JSP and servlet
1, first need a JSP page to select the product
The procedure is as follows:
1<%@ page language= "Java"Import= "java.util.*" pageencoding= "GBK" contenttype= "text/html; CHARSET=GBK "%>2<%3String Path =Request.getcontextpath ();4String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";5%>6 7<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >89Ten<base href= "<%=basePath%>" > One A<title>my JSP ' buy.jsp ' starting page</title> - -<meta http-equiv= "Pragma" content= "No-cache" > the<meta http-equiv= "Cache-control" content= "No-cache" > -<meta http-equiv= "Expires" content= "0" > -<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" > -<meta http-equiv= "description" content= "This is my page" > +<!-- -<link rel= "stylesheet" type= "Text/css" href= "Styles.css" > +- A at - -<body> -<form action= "Buycar" method= "POST" > -Computer: <input type= "checkbox" name= "goods" value= "Computer" ><br/> -Mobile: <input type= "checkbox" name= "goods" value= "phone" ><br/> inWatch: <input type= "checkbox" name= "goods" value= "Watch" ><br/> -<input type= "Submit" value= "buy"/> to</form> + - the</body> *2, requires a servlet serverThe procedure is as follows:
1 Packageservlet;2 3 Importjava.io.IOException;4 ImportJava.util.HashMap;5 6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.annotation.WebServlet;8 ImportJavax.servlet.http.HttpServlet;9 Importjavax.servlet.http.HttpServletRequest;Ten ImportJavax.servlet.http.HttpServletResponse; One Importjavax.servlet.http.HttpSession; A -@WebServlet (urlpatterns={"/buycar"}) - Public classShopcarextendshttpservlet{ the PrivateHttpSession hts; -Hashmap<string, integer> hs=NewHashmap<string, integer>(); - Static intSumcom=0; - Static intSumphone=0; + Static intSumwatch=0; - @Override + protected voidService (HttpServletRequest req, httpservletresponse Res) A throwsservletexception, IOException { atReq.setcharacterencoding ("GBK"); -String[] Goodshop=req.getparametervalues ("goods"); - for(String goods:goodshop) { - if("Computer". Equals (goods)) { -Hs.put ("PC", + +sumcom); - } in if("Phone". Equals (goods)) { -Hs.put ("phone", + +sumphone); to } + if("Watch". Equals (goods)) { -Hs.put ("Watch", + +sumwatch); the } * } $hts=req.getsession ();Panax NotoginsengHts.setattribute ("Shoppingcar", HS); -Req.getrequestdispatcher ("shoppcar.jsp"). Forward (req, res); the } + A}
3, need a product to buy the display pageThe procedure is as follows:
1<%@ page language= "Java"Import= "java.util.*" pageencoding= "GBK" contenttype= "text/html; CHARSET=GBK "%>2<%3String Path =Request.getcontextpath ();4String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";5%>6 7<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >89Ten<base href= "<%=basePath%>" > One A<title>my JSP ' shoppcar.jsp ' starting page</title> - -<meta http-equiv= "Pragma" content= "No-cache" > the<meta http-equiv= "Cache-control" content= "No-cache" > -<meta http-equiv= "Expires" content= "0" > -<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" > -<meta http-equiv= "description" content= "This is my page" > +<!-- -<link rel= "stylesheet" type= "Text/css" href= "Styles.css" > +- A at - -<body> -<% -Request.setcharacterencoding ("GBK"); -Hashmap<string,integer> hs= (hashmap<string,integer>) session.getattribute ("ShoppingCar"); in if(hs!=NULL){ -Set set=Hs.keyset (); toIterator<string> it=set.iterator (); + while(It.hasnext ()) { -String key=It.next (); the if(Key.equals ("Computer")){ *Out.print ("Buy Computer" +hs.get (key) + "Taiwan"); $ }Panax Notoginseng if(Key.equals ("Phone")){ -Out.print ("Buy mobile" +hs.get (key) + "Taiwan"); the } + if(Key.equals ("Watch")){ AOut.print ("Buy watch" +hs.get (key) + "Taiwan"); the } + } - } $ $ - - the%> - Wuyi<a hres= "buy.jsp" > Continue to purchase </a> the</body> -The shopping cart of JSP