当前位置: 首页 > >

简单购物车和登录界面的java程序

发布时间:

CartBean.java package bean; import java.util.ArrayList; import java.util.Iterator; public class CartBean { ArrayList items; public CartBean(){ this.items=new ArrayList(); } public void addItem(GoodsBean item){ items.add(item); } public ArrayList getItems(){ return this.items; } public int getTotalPrice(){ int total=0; Iterator it=items.iterator(); while(it.hasNext()){ GoodsBean item=(GoodsBean)it.next(); total=total+item.getPrice()*item.count; } return total; } }

GoodsBean.java package bean; public class GoodsBean { String id; String name; int price; int count; public GoodsBean(){} public GoodsBean(String id,String price,int count){ this.id=id; ...... } public String getId() { return id;} public void setId(String id) { this.id = id;} ...... }

name,int

CartServlet.java package servlet; import java.io.IOException; import javax.servlet.*; import javax.servlet.http.*; public class CartServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter("id"); String name=request.getParameter("name"); int price=Integer.parseInt(request.getParameter("price")); int count=Integer.parseInt(request.getParameter("count")); bean.GoodsBean item=new bean.GoodsBean(id,name,price,count); bean.CartBean cb=(bean.CartBean)request.getSession().getAttribute("cb"); if(cb==null) cb=new bean.CartBean(); cb.addItem(item); request.getSession().setAttribute("cb",cb); response.sendRedirect("shopping.jsp"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }

index.jsp <%@ page contentType="text/html; charset=gb2312" language="java" %> <html> <head> <title>购物界面</title> <style type="text/css"> <!-.STYLE1 {color: #000099} --> </style> </head> <body> <table width="371" height="138" border="1"> <tr> <td width="67" bgcolor="#CCCCCC"><div align="center" class="STYLE1">商品序号</div></td> ......商品名称、商品单价、购买数量、购买操作 </tr> <form name="myForm1" action="CartServlet?id=001&name=apple&price=12" method="post"> <tr> shopping.jsp <td>001</td> <%@ page contentType="text/html; charset=gb2312" language="java" %> <td>apple</td> <%@ page import="bean.GoodsBean" %> <td>12</td> <%@ page import="bean.CartBean" %> <td><div align="center"> <%@ page import="java.util.*" %> <input name="count" type="text" size="5"> <html> </div></td> <head> <td><div align="center"> <title>shopping.jsp</title> <input type="submit" value="购买"> </head> </div></td> <body> </tr> <% </form> CartBean cb=(CartBean)session.getAttribute("cb"); ......002.003.004 if(cb!=null){ </table> ArrayList items=cb.getItems(); <br><a href="shopping.jsp">查看购物车</a> for(Iterator it=items.iterator();it.hasNext();){ </body> GoodsBean item=(GoodsBean)it.next(); </html> out.println("商品序号"+item.getId()); out.println("商品名称"+item.getName()); out.println("商品价格"+item.getPrice()); out.println("商品数量"+item.getCount()); out.println("<br>"); } out.println("商品总价"+cb.getTotalPrice()); }else{ out.println("购物车没有商品"); } %> <br> <a href="index.jsp">继续购物</a> </body> </html>

UserCheckBean.java package bean; public class UserCheckBean { protected UserBean user; public UserCheckBean() {} public UserCheckBean(UserBean user) { this.user = user;} public UserBean getUser() { return user;} public void setUser(UserBean user) { this.user = user;} public boolean validate(){ String name=user.getName(); String pwd=user.getPwd(); if("zhangsan".equals(name)&&"123".equals(pwd)){ return true; }else{ return false;} } }

UserBean.java package bean; public class UserBean { private String name; private String pwd; public String getName() { return name;} public void setName(String name) { this.name = name;} public String getPwd() { return pwd;} public void setPwd(String pwd) { this.pwd = pwd;} }

ControllerServlet.java package servlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import bean.*; public class ControllerServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("GBK"); response.setContentType("text/html;charset=GBK"); UserBean user=new UserBean(); user.setName(request.getParameter("name")); user.setPwd(request.getParameter("pwd")); UserCheckBean uc=new UserCheckBean(user); RequestDispatcher rd; if(uc.validate()){ HttpSession session=request.getSession(); session.setAttribute("user", user); rd=request.getRequestDispatcher("welcome.jsp"); rd.forward(request, response); }else{ rd=request.getRequestDispatcher("loginer.jsp"); rd.forward(request, response);} } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response);} } loginer.jsp <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> 用户名或密码错误,请<a href=\"login.html\">重新登录</a>");




友情链接: 高中资料网 职业教育网 成人教育网 理学 大学工学资料