[原创]java WEB学习笔记33:Session 案例 之 购物车

本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1. 简易session版购物车:创建一个简单的购物车模型,由三个 jsp 和两个 Servlet 组成:

[原创]java WEB学习笔记33:Session 案例 之 购物车

checkbox 是一组的话 name必须一致

代码:

  1)step-1.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>step-1.jsp</title>
</head>
<body>
<h4>Step1:选择要购买的图书</h4> <form action="<%= request.getContextPath() %>/processStep1" method="post" >
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>书名</td>
<td>购买</td>
</tr> <tr>
<td>Java</td>
<td><input type="checkbox" name="book" value="Java"/></td>
</tr> <tr>
<td>Oracle</td>
<td><input type="checkbox" name="book" value="Oracle"/></td>
</tr> <tr>
<td>Struts</td>
<td><input type="checkbox" name="book" value="Struts"/></td>
</tr> <tr>
<td colspan="2" >
<input align="center" type="submit" value="Submit"/>
</td> </tr> </table>
</form> </body>
</html>

        [原创]java WEB学习笔记33:Session 案例 之 购物车

  2)step-2.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Step-2.jsp</title>
</head>
<body>
<form action="<%= request.getContextPath() %>/processStep2" method="post">
<h4>step2: 请输入寄送的地址和信用卡信息</h4>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td colspan="2" align="center">寄送信息</td>
</tr> <tr>
<td>姓名:</td>
<td><input type="text" name="name"/></td>
</tr> <tr>
<td>寄送地址:</td>
<td><input type="text" name="address"/></td>
</tr> <tr>
<td colspan="2" align="center">信用卡信息</td>
</tr> <tr>
<td>种类:</td>
<td>
<input type="radio" name="cardType" value="Visa"/>Visa
<input type="radio" name="cardType" value="Master"/>Master </td>
</tr> <tr>
<td>卡号:</td>
<td><input type="text" name="card"/></td>
</tr> <tr>
<td colspan="2">
<input align="center" type="submit" value="Submit"/></td>
</tr>
</table>
</form>
</body>
</html>

      [原创]java WEB学习笔记33:Session 案例 之 购物车

  3)confim.jsp

 <%@page import="com.jason.shoopingcart.bean.Customer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%
Customer customer = (Customer)session.getAttribute("customer");
String[] books = (String[])session.getAttribute("books");
%>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>顾客姓名:</td>
<td><%= customer.getName() %></td>
</tr> <tr>
<td>地址:</td>
<td><%= customer.getAddress() %></td>
</tr>
<tr>
<td>卡号:</td>
<td><%= customer.getCard()%></td>
</tr>
<tr>
<td>卡的类型:</td>
<td><%= customer.getCardType()%></td>
</tr>
<tr>
<td>买的书:</td>
<td>
<%
for(String book : books ){
out.print(book);
out.print("<br>");
}
%>
</td>
</tr>
</table> </body>
</html>

  

  4)ProcessStep1Servlet.java

  1 package com.jason.shoopingcart.servlet;
2
3 import java.io.IOException;
4 import javax.servlet.ServletException;
5 import javax.servlet.annotation.WebServlet;
6 import javax.servlet.http.HttpServlet;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 /**
11 * Servlet implementation class ProcessStep1Servlet
12 */
13 @WebServlet("/processStep1")
14 public class ProcessStep1Servlet extends HttpServlet {
15 private static final long serialVersionUID = 1L;
16
17
18 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
19
20 //1.获取选中的图书的信息
21 String[] books = request.getParameterValues("book");
22
23 //2.把图书信息放入到HttpSession 中
24 request.getSession().setAttribute("books", books);
25
26 //3.重定向到shoppingcart/step-2.jsp
27 response.sendRedirect(request.getContextPath() + "/shoppingcart/step-2.jsp");
28
29 }
30
31 }

  

  5)ProcessStep2Servlet.java

 1 package com.jason.shoopingcart.servlet;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.annotation.WebServlet;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10 import javax.servlet.http.HttpSession;
11
12 import com.jason.shoopingcart.bean.Customer;
13
14 /**
15 * Servlet implementation class ProcessStep2Servlet
16 */
17 @WebServlet("/processStep2")
18 public class ProcessStep2Servlet extends HttpServlet {
19 private static final long serialVersionUID = 1L;
20
21
22 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
23 //1. 获取请求参数 name ,address ,cardType,card
24 request.setCharacterEncoding("UTF-8");
25 String name = request.getParameter("name");
26 String address = request.getParameter("address");
27 String cardType = request.getParameter("cardType");
28 String card = request.getParameter("card");
29
30 Customer customer = new Customer(name, address, cardType, card);
31
32 //2.把请求存储到Httpsession中
33 HttpSession session = request.getSession();
34 session.setAttribute("customer", customer);
35
36 //3.重定向到confirm.jsp
37 response.sendRedirect(request.getContextPath() + "/shoppingcart/confirm.jsp");
38 }
39
40 }

   

  6)Customer.java

 package com.jason.shoopingcart.bean;

 public class Customer {
private String name;
private String address;
private String cardType;
private String card; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getCardType() {
return cardType;
} public void setCardType(String cardType) {
this.cardType = cardType;
} public void setCard(String card) {
this.card = card;
}
public String getCard() {
return card;
} public Customer(String name, String address, String cardType, String card) {
super();
this.name = name;
this.address = address;
this.cardType = cardType;
this.card = card;
} public Customer() {
super();
} }
上一篇:Google JavaScript规范


下一篇:C++ Primer 学习笔记_61_重载操作符与转换 --自增/自减操作符