一.简易购物车
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>Insert title here</title>
</head>
<body>
<h1>选择购买的图书</h1>
<form action="<%=request.getContextPath() %>/processStep1" method="post">
<table border="1" width="300px">
<tr>
<td>书名</td>
<td>购买</td>
</tr>
<tr>
<td>JAVA</td>
<td><input type="checkbox" name="book" value="Java"></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>JS</td>
<td><input type="checkbox" name="book" value="JS"></td>
</tr>
<tr>
<td>Andried</td>
<td><input type="checkbox" name="book" value="Andried"></td>
</tr>
<tr>
<td>PHP</td>
<td><input type="checkbox" name="book" value="PHP"></td>
</tr>
<tr>
<td><input type="submit" vaiue="submit"></td>
</tr>
</table>
</form>
</body>
</html>
2.ProcessStep1Servlet.java
package com.atgugu.shopping;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet({ "/ProcessStep1Servlet", "/processStep1" })
public class ProcessStep1Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取选中的图书信息
String[] books=request.getParameterValues("book");// 得到一组数据
//2.把取到的放在Session里面
request.getSession().setAttribute("book's", books);
//3.重定向到shoppingcart/step-2.jsp
response.sendRedirect(request.getContextPath()+"/shoppingcart/step-2.jsp");//相对路径
}
}
3.Customer.java
package com.atgugu.shopping;
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 String getCard() {
return card;
}
public void setCard(String card) {
this.card = 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();
}
}
4.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>Insert title here</title>
</head>
<body>
<h1>Step2:请输入信息</h1>
<form action="<%=request.getContextPath()%>/processStep2" method="post">
<table cellpadding="10" cellspacing="0" border="1">
<tr>
<td colspan="2">寄送信息</td>
</tr>
<tr>
<td >寄送地址</td>
<td><input type="text" name="address"></td>
</tr>
<tr>
<td colspan="2">信用卡信息</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 type="submit" value="submit"> </td>
</tr>
</table>
</form>
</body>
</html>
5.ProcessStep2Servlet.java
package com.atgugu.shopping;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet({ "/ProcessStep2Servlet", "/processStep2" })
public class ProcessStep2Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取请求参数:name,address,cardType,card
String name=request.getParameter("name");
String address=request.getParameter("address");
String cardType=request.getParameter("cardType");
String card=request.getParameter("card");
Customer customer=new Customer(name,address,cardType,card);
//2.把请求信息存入到Session 中
HttpSession session=request.getSession();
session.setAttribute("customer", customer);
//3.重定向页面到confir.jsp
response.sendRedirect(request.getContextPath()+"/shoppingcart/confirm.jsp");
}
}
6.confirm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.atgugu.shopping.Customer" %>
<!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 h2ere</title>
</head>
<body>
<%
Customer customer=(Customer)request.getAttribute("customer");
String [] books=(String[])session.getAttribute("books");
%>
<table>
<tr>
<td>顾客姓名</td>
<td><%=customer.getName() %></td>
</tr>
<tr>
<td>地址</td>
<td><%=customer.getAddress() %></td>
</tr>
<tr>
<td>卡类型</td>
<td><%=customer.getCardType()%></td>
</tr>
<tr>
<td>Books</td>
<td>
<%
for(String book:books){
out.print(book);
out.print("<br>");
}
%>
</td>
</tr>
</table>
</body>
</html>