
简单的购物车实现.pdf
6页简单的购物车实现这是我个人写的简单的Javaweb程序,简单的购物车实现以下为主要源程序代码/以下代码是一个简单的选购物品页面shop.html !-电视机 苹果 大米 酱油 台斤瓶袋 数量:价钱:元 名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 6 页 -简单的购物车实现 /这是一个简单的购物Servlet 程序package cn;import java.io.IOException;import java.io.PrintWriter;import java.util.Enumeration;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class Shop extends HttpServlet /*Constructor of the object.*/public Shop()super();/*Destruction of the servlet.*/public void destroy()super.destroy();/Just puts destroy string in log/Put your code here 名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 6 页 -简单的购物车实现/*The doGet method of the servlet.*This method is called when a form has its tag value method equals to get.*param request the request send by the client to the server*param response the response send by the server to the client*throws ServletException if an error occurred*throws IOException if an error occurred*/*The doPost method of the servlet.*This method is called when a form has its tag value method equals to post.*param request the request send by the client to the server*param response the response send by the server to the client*throws ServletException if an error occurred*throws IOException if an error occurred*/public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException /*response.setContentType(text/html);PrintWriter out=response.getWriter();HttpSession session=request.getSession(true);Goods s=new Goods();s.setGoods(request.getParameter(goods);s.setLei(request.getParameter(lei);s.setNumber(request.getParameter(number);s.setPrice(request.getParameter(price);session.setAttribute(request.getParameter(goods),s);System.out.println(session);Enumeration e=request.getAttributeNames();out.println();out.println(A Servlet);out.println();out.print(购物车);while(e.hasMoreElements()String name=e.nextElement();Goods g=(Goods)session.getAttribute(goods);out.print(商品名称:+g.getGoods()+);名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 6 页 -简单的购物车实现out.print(商品:+g.getLei()+);out.print(商品数量:+g.getNumber()+);out.print(商品价格:+g.getPrice()+);out.print();out.println(继续购物);out.println();out.println();out.flush();out.close();*/response.setContentType(text/html;charset=utf-8);request.setCharacterEncoding(utf-8);HttpSession session=request.getSession();String goods=request.getParameter(goods);String price=request.getParameter(price);String number=request.getParameter(number);String lei=request.getParameter(lei);Goods good=new Goods(goods,number,price,lei);session.setAttribute(good.toString(),good);/session.setAttribute(goods,good);PrintWriter out=response.getWriter();Enumeration e=session.getAttributeNames();while(e.hasMoreElements()String name=e.nextElement();Goods g1=(Goods)session.getAttribute(name);out.println(商品:+g1.getGoods()+);out.println(数量:+g1.getNumber()+g1.getLei()+);out.println(价格:+g1.getPrice()+);out.println();/out.println(单位:+g1.getLei()+);out.println();out.println(A Servlet);out.println();out.println(继续购物 );out.println();out.println();out.flush();名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 6 页 -简单的购物车实现out.close();/*Initialization of the servlet.*throws ServletException if an error occurs*/public void init()throws ServletException /Put your code here /以下代码是声名商品的属性。
package cn;public class Goods private String goods;private String number;private String price;private String lei;public Goods()public Goods(String goods,String number,String price,String lei)this.goods=goods;this.number=number;this.price=price;this.lei=lei;public String getGoods()return goods;public void setGoods(String goods)this.goods=goods;public String getNumber()return number;public void setNumber(String number)this.number=number;名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 6 页 -简单的购物车实现 public String getPrice()return price;public void setPrice(String price)this.price=price;public String getLei()return lei;public void setLei(String lei)this.lei=lei;public String toString()return goods+number+price+lei;/以下代码为Servlet 程序在 web.xml 下的配置,这个可以直接创建servlet 类直接自动生成 This is the description of my J2EE component This is the display name of my J2EE component Shop cn.Shop Shop/servlet/Shop index.jsp 名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 6 页 -。






![河南新冠肺炎文件-豫建科[2020]63号+豫建科〔2019〕282号](http://img.jinchutou.com/static_www/Images/s.gif)





