本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用
内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。
本人互联网技术爱好者,互联网技术发烧友
微博:伊直都在0221
QQ:951226918
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. 简易session版购物车:创建一个简单的购物车模型,由三个 jsp 和两个 Servlet 组成:
checkbox 是一组的话 name必须一致
代码:
1)step-1.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 5 6 744 45 46step-1.jsp 8 9 10Step1:选择要购买的图书
11 12
2)step-2.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 5 6 751 52Step-2.jsp 8 9 10
3)confim.jsp
1 <%@page import="com.jason.shoopingcart.bean.Customer"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 5 6 7 8Insert title here 9 10 11 12 <%13 Customer customer = (Customer)session.getAttribute("customer");14 String[] books = (String[])session.getAttribute("books");15 %>16
顾客姓名: | 19<%= customer.getName() %> | 20
地址: | 24<%= customer.getAddress() %> | 25
卡号: | 28<%= customer.getCard()%> | 29
卡的类型: | 32<%= customer.getCardType()%> | 33
买的书: | 3637 <%38 for(String book : books ){39 out.print(book);40 out.print("");41 }42 %>43 | 44
4)ProcessStep1Servlet.java
1 1 package com.jason.shoopingcart.servlet; 2 2 3 3 import java.io.IOException; 4 4 import javax.servlet.ServletException; 5 5 import javax.servlet.annotation.WebServlet; 6 6 import javax.servlet.http.HttpServlet; 7 7 import javax.servlet.http.HttpServletRequest; 8 8 import javax.servlet.http.HttpServletResponse; 9 9 10 10 /**11 11 * Servlet implementation class ProcessStep1Servlet12 12 */13 13 @WebServlet("/processStep1")14 14 public class ProcessStep1Servlet extends HttpServlet {15 15 private static final long serialVersionUID = 1L;16 16 17 17 18 18 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {19 19 20 20 //1.获取选中的图书的信息21 21 String[] books = request.getParameterValues("book");22 22 23 23 //2.把图书信息放入到HttpSession 中24 24 request.getSession().setAttribute("books", books);25 25 26 26 //3.重定向到shoppingcart/step-2.jsp27 27 response.sendRedirect(request.getContextPath() + "/shoppingcart/step-2.jsp");28 28 29 29 }30 30 31 31 }
5)ProcessStep2Servlet.java
1 1 package com.jason.shoopingcart.servlet; 2 2 3 3 import java.io.IOException; 4 4 5 5 import javax.servlet.ServletException; 6 6 import javax.servlet.annotation.WebServlet; 7 7 import javax.servlet.http.HttpServlet; 8 8 import javax.servlet.http.HttpServletRequest; 9 9 import javax.servlet.http.HttpServletResponse;10 10 import javax.servlet.http.HttpSession;11 11 12 12 import com.jason.shoopingcart.bean.Customer;13 13 14 14 /**15 15 * Servlet implementation class ProcessStep2Servlet16 16 */17 17 @WebServlet("/processStep2")18 18 public class ProcessStep2Servlet extends HttpServlet {19 19 private static final long serialVersionUID = 1L;20 20 21 21 22 22 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {23 23 //1. 获取请求参数 name ,address ,cardType,card24 24 request.setCharacterEncoding("UTF-8");25 25 String name = request.getParameter("name");26 26 String address = request.getParameter("address");27 27 String cardType = request.getParameter("cardType");28 28 String card = request.getParameter("card");29 29 30 30 Customer customer = new Customer(name, address, cardType, card);31 31 32 32 //2.把请求存储到Httpsession中33 33 HttpSession session = request.getSession();34 34 session.setAttribute("customer", customer);35 35 36 36 //3.重定向到confirm.jsp37 37 response.sendRedirect(request.getContextPath() + "/shoppingcart/confirm.jsp");38 38 }39 39 40 40 }
6)Customer.java
1 package com.jason.shoopingcart.bean; 2 3 public class Customer { 4 private String name; 5 private String address; 6 private String cardType; 7 private String card; 8 9 public String getName() {10 return name;11 }12 13 public void setName(String name) {14 this.name = name;15 }16 17 public String getAddress() {18 return address;19 }20 21 public void setAddress(String address) {22 this.address = address;23 }24 25 public String getCardType() {26 return cardType;27 }28 29 public void setCardType(String cardType) {30 this.cardType = cardType;31 }32 33 public void setCard(String card) {34 this.card = card;35 }36 public String getCard() {37 return card;38 }39 40 41 42 public Customer(String name, String address, String cardType, String card) {43 super();44 this.name = name;45 this.address = address;46 this.cardType = cardType;47 this.card = card;48 }49 50 public Customer() {51 super();52 }53 54 }