日期:2014-05-16 浏览次数:20370 次
package cn.itcast.cd.jsp.domain; public class User { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [name=" + name + ", password=" + password + "]"; } } package cn.itcast.cd.jsp.domain; import java.util.ArrayList; import java.util.List; public class ShoppingCar { private List<ShoppingCarItem> list = new ArrayList<ShoppingCarItem>(); public List<ShoppingCarItem> getList() { return list; } public void add(ShoppingCarItem item){ list.add(item); } public void remove(String id){ for (ShoppingCarItem item : list) { if (id.equals(item.getId())){ list.remove(item); break; } } } } package cn.itcast.cd.jsp.domain; public class ShoppingCarItem { private String id; private String name; private double price; private Integer number; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Integer getNumber() { return number; } public void setNumber(Integer number) { this.number = number; } } package cn.itcast.cd.utils; public class Utils { public static boolean hasLength(String str) { return str != null && !"".equals(str); } }
package cn.itcast.cd.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import cn.itcast.cd.jsp.domain.User; /** * Servlet implementation class LoginServlet */ public class LoginServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); //获取参数 String name = request.getParameter("name"); String password = request.getParameter("password"); //封装对象 User user = new User(); user.setPassword(password); user.setName(name); HttpSession session = request.getSession(); session.setAttribute("user", user); //显示到页面 RequestDispatcher dispatcher = request.getRequestDispatcher("/welcome.jsp"); dispatcher.forward(request, response); } } package cn.itcast.cd.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import cn.itcast.cd.jsp.domain.ShoppingCar; import cn.itcast.cd.jsp.domain.ShoppingCarItem; /** * Servlet implementation class BuyServlet */ public class BuyServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String number = request.getParameter("number"); ShoppingCarItem item = new ShoppingCarItem(); item.setName(name); item.setNumber(Integer.parseInt(number)); HttpSession session = request.getSession(); ShoppingCar car = (ShoppingCar)session.getAttribute("car"); if (car == null) { //car为空,则创建一个购物车,并放到session中,否则,session中有购物车,不需要再添加到session中.