The shopping cart of JSP

Source: Internet
Author: User
Tags set set

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 server

The 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 page

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 ' 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.