例子:利用session完成购物车.
domain:
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); } }
servlet:
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中. car = new ShoppingCar(); session.setAttribute("car", car); } car.add(item); RequestDispatcher dispatcher = request.getRequestDispatcher("/car.jsp"); dispatcher.forward(request, response); } } package cn.itcast.cd.servlet; import java.io.IOException; import java.util.List; 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 ShowServlet */ public class ShowServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); HttpSession session = request.getSession(); ShoppingCar car = (ShoppingCar)session.getAttribute("car"); RequestDispatcher dispatcher = request.getRequestDispatcher("/car.jsp"); dispatcher.forward(request, response); } }
登陆页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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>Insert title here</title> </head> <body> <form action="/login" method="post"> <table border="0" cellpadding="3" cellspacing="0"> <tr> <td>用户名:</td> <td><input type="text" name="name"/></td> </tr> <tr> <td>密 码:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="登 陆"/> </td> </tr> </table> </form> </body> </html>
欢迎页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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>Insert title here</title> </head> <body> 欢迎回来!<font color="blue"><b><i>${sessionScope.user.name}</i></bi></font><br/> <a href="/buy.jsp">开始购物</a> <a href="/show">管理购物车</a> </body> </html>
购买页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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>Insert title here</title> </head> <body> <form action="/buy" method="post"> <table border="0" cellpadding="3" cellspacing="0"> <tr> <td>商品名称:</td> <td> <select name="name"> <option>鼠标</option> <option>键盘</option> <option>显示器</option> </select> </td> </tr> <tr> <td>数  量:</td> <td><input type="text" name="number"/></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="购买"/> </td> </tr> </table> </form> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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>Insert title here</title> </head> <body> <c:choose> <c:when test="${empty sessionScope.car}"> 购物车没有任何商品...... </c:when> <c:otherwise> <table> <tr> <th>商品名称</th> <th>商品数量</th> </tr> <c:forEach items="${sessionScope.car.list}" var="item"> <tr> <td>${item.name }</td> <td>${item.number }</td> </tr> </c:forEach> </table> </c:otherwise> </c:choose> <form action="/buy.jsp" method="post"> <input type ="submit" value="继续购物"/> </form> </body> </html>