(1) using the servlet to implement a simple shopping cart system, the project structure is as follows: (new Web project projects only need Additemservlet, listitemservlet,exam403.jsp three files, others without tube)
(2) The EXAM403.JSP code is as follows:
<%@ 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) The Additemservlet code is as follows: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 a product 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//If the incoming Product ID number is empty or the quantity is empty, display the hint 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) The Listitemservlet code is as follows: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) The following results are achieved:Visit: 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