SERVLET/JSP Realization Shopping Cart

Source: Internet
Author: User

(1) using the servlet to implement a simple shopping cart system, the project structure such as the following: (New Web project projects only need Additemservlet, Listitemservlet. Exam403.jsp three files is available. Other no-tube)

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvegxnzw4xntczodc=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

(2) Exam403.jsp code such as the following:

<%@ page contenttype= "text/html; charset=gb2312 "language=" java "import=" java.sql.* "errorpage=" "%><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
(3) Additemservlet code such as the following:

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 R Esponse) throws Servletexception,java.io.ioexception {ServletContext Application=getservle      Tcontext ();      ServletConfig Config=getservletconfig ();      Response.setcontenttype ("text/html;charset=gb2312");       PrintWriter Out=response.getwriter ();      HttpSession session =request.getsession ();            Request.setcharacterencoding ("gb2312");  Read the incoming Product ID and number of the form String id=request.getparameter ("ItemID"); String num=request.getparameter ("Quantity"); if (Id!=null && num.length ()!=0) {//read from Sessionn shopping cart HashMap shoppingcar= (HashMap) Session.getattribute ("    Shoppingcar ");    if (shoppingcar==null) shoppingcar=new HashMap ();    Add the item to the shopping cart 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);    }//The shopping cart is written back to the session to save Session.setattribute ("Shoppingcar", Shoppingcar); } else//Assume that the incoming Product ID number is empty or the quantity is empty. Display the message System.out.print ("The Product ID number is empty or the quantity is empty!")  ");         Return to Product List page response.sendredirect ("/servletproject/exam403.jsp"); } protected void DoPost (HttpServletRequest request,httpservletresponse response) throw  S servletexception,java.io.ioexception {doget (request,response); }}

(4) Listitemservlet code such as the following:

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=GETSERVL      Etcontext ();      ServletConfig Config=getservletconfig ();      Response.setcontenttype ("text/html;charset=gb2312");       PrintWriter Out=response.getwriter ();      HttpSession session =request.getsession ();             Request.setcharacterencoding ("gb2312");  Get shopping cart from session HashMap shoppingcar= (HASHMAP) session.getattribute ("Shoppingcar");      Displays the contents of the shopping cart if (shoppingcar!=null) {Set show=shoppingcar.entryset ();      Iterator It=show.iterator ();       while (It.hasnext ()) {Out.print (It.next () + "<br>"); }} else Out.print ("The shopping cart is empty.       "); } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception,java.io.ioexception {doget (req  Uest,response); }}

(5) Implementation effects such as the following:

Interview: Http://localhost:8080/servletProject/exam403.jsp product submission


Click to view Cart:


Ok!

This is the end of a simple shopping cart!

SERVLET/JSP Realization Shopping Cart

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.