1. 根据需求抽象出业务对象
User{
usrename;
password;
}
ShoppingCarItem{
product;
num;
}
ShoppingCar{
List<ShoppingCarItem>
}
2. 写Servlet处理请求
...
domain:
- package cn.itcast.cd.domain;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.UUID;
- public class ShoppingCar {
- private List<ShoppingCarItem> list = new ArrayList<ShoppingCarItem>();
- /**
- * 根据货品和数量往购物车中添加
- * @param product
- * @param num
- */
- public void add(String product,Integer num){
- ShoppingCarItem carItem = new ShoppingCarItem();
- carItem.setNum(num);
- carItem.setProduct(product);
- carItem.setId(UUID.randomUUID().toString());
- list.add(carItem);
- }
- /**
- * 根据唯一的标示删除购物车中对应的明细
- * @param id
- */
- public void remove(String id){
- for (ShoppingCarItem item : list) {
- if(id.equals(item.getId())){
- list.remove(item);
- break;
- }
- }
- }
- public List<ShoppingCarItem> getList() {
- return list;
- }
- }
- package cn.itcast.cd.domain;
- public class ShoppingCarItem {
- private String product;
- private Integer num;
- private String id; //给每一个明细生成一个唯一的id
- public String getProduct() {
- return product;
- }
- public void setProduct(String product) {
- this.product = product;
- }
- public Integer getNum() {
- return num;
- }
- public void setNum(Integer num) {
- this.num = num;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- }
- package cn.itcast.cd.domain;
- public class User {
- private String username;
- private String password;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
servlet:
- package cn.itcast.cd.servlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- 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.domain.ShoppingCar;
- public class BuyServlet extends HttpServlet {
- @Override
- protected void service(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("UTF-8");
- response.setContentType("text/html;charset=utf-8");
- //1. 获取请求中的参数.
- String product = request.getParameter("product");
- String num = request.getParameter("num");
- //2. 获取到购物车
- //2.1 首先要到session中找一下是否已经有了购物车. 如果有的话就不需要创建了... 直接把货品放到购物车中就可以了
- HttpSession httpSession = request.getSession();
- ShoppingCar shoppingCar = (ShoppingCar)httpSession.getAttribute("SHOPINGCAR_IN_SESSION");
- if(shoppingCar==null){ //说明是第一次购物. 还没有购物车的
- //2.2 如果从session中没有找到购物车. 需要新创建一个购物车并且放到session中.为了下次购物使用这个购物车
- shoppingCar = new ShoppingCar();
- httpSession.setAttribute("SHOPINGCAR_IN_SESSION", shoppingCar);
- }
- shoppingCar.add(product, Integer.parseInt(num));
- PrintWriter pw = response.getWriter();
- pw.println("添加成功<br/>");
- pw.println("<a href='/buy.html'>继续购物</a><br/>");
- pw.println("<a href='/list'>管理购物</a><br/>");
- }
- }
- package cn.itcast.cd.servlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- 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.domain.ShoppingCar;
- import cn.itcast.cd.domain.ShoppingCarItem;
- import cn.itcast.cd.domain.User;
- public class ListServlet extends HttpServlet {
- @Override
- protected void service(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("UTF-8");
- response.setContentType("text/html;charset=utf-8");
- HttpSession httpSession = request.getSession();
- //1. 先获取session中的User
- User user = (User) httpSession.getAttribute("USER_IN_SESSION");
- PrintWriter pw = response.getWriter();
- pw.println("当前用户为:"+user.getUsername()+"<br/>");
- //2. 获取session的购物车
- ShoppingCar shoppingCar = (ShoppingCar) httpSession.getAttribute("SHOPINGCAR_IN_SESSION");
- //2.1 如果在session中购物车的话,列出购物车中的明细
- if(shoppingCar!=null){
- for (ShoppingCarItem item : shoppingCar.getList()) {
- pw.println("货品:"+item.getProduct()+" 数量:"+item.getNum()+"<br/>");
- }
- }else{
- //2.2 在session中没有购物车的话.提示用户没有当前用户名购物车
- pw.println("当前用户没有购物车.");
- }
- }
- }
- package cn.itcast.cd.servlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- 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.domain.User;
- public class LoginServlet extends HttpServlet {
- @Override
- protected void service(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("UTF-8");
- response.setContentType("text/html;charset=utf-8");
- //1. 接收请求中的数据封装对象
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- User user = new User();
- user.setPassword(password);
- user.setUsername(username);
- //2. 将用户方到session*ListServlet从session中访问到User
- HttpSession httpSession = request.getSession();
- httpSession.setAttribute("USER_IN_SESSION", user);
- PrintWriter pw = response.getWriter();
- pw.println("欢迎:"+username+"<br/>");
- pw.println("<a href='/buy.html'>购物</a><br/>");
- pw.println("<a href='/list'>管理购物</a><br/>");
- }
- }
用户登录页面:
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>用户登录</title>
- </head>
- <body>
- <form action="/login" method="post">
- 用户名:<input type="text" name="username"><br/>
- 密码:<input type="text" name="password"><br/>
- <input type="submit" value="登录">
- </form>
- </body>
- </html>
购买页面:
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>购物货品</title>
- </head>
- <body>
- <form action="/buy" method="post">
- 货品:<select name="product">
- <option value="鼠标">鼠标</option>
- <option value="键盘">键盘</option>
- <option value="内存">内存</option>
- <option value="显示器">显示器</option>
- </select> <br/>
- 数量:<input type="text" name="num"><br/>
- <input type="submit" value="购买">
- </form>
- </body>
- </html>
web.xml配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
- <servlet>
- <servlet-name>LoginServlet</servlet-name>
- <servlet-class>cn.itcast.cd.servlet.LoginServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>LoginServlet</servlet-name>
- <url-pattern>/login</url-pattern>
- </servlet-mapping>
- <servlet>
- <servlet-name>BuyServlet</servlet-name>
- <servlet-class>cn.itcast.cd.servlet.BuyServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>BuyServlet</servlet-name>
- <url-pattern>/buy</url-pattern>
- </servlet-mapping>
- <servlet>
- <servlet-name>ListServlet</servlet-name>
- <servlet-class>cn.itcast.cd.servlet.ListServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>ListServlet</servlet-name>
- <url-pattern>/list</url-pattern>
- </servlet-mapping>
- </web-app>